| 1 |
lars |
1 |
<com:TContent ID="body" >
|
|
|
2 |
<h1 id="6801">Javascript in PRADO, Questions and Answers</h1>
|
|
|
3 |
<h2 id="6802">How do I include the Javascript libraries distributed with Prado?</h2>
|
|
|
4 |
<p id="880060" class="block-content">The javascript libraries distributed with Prado can be found in the
|
|
|
5 |
<tt>framework/Web/Javascripts/source</tt> directory. The <tt>packages.php</tt>
|
|
|
6 |
file in that directory defines a list of available package names available
|
|
|
7 |
to be loaded. They can be loaded as follows.
|
|
|
8 |
</p>
|
|
|
9 |
<ul id="u1" class="block-content"><li>Adding libraries in the template
|
|
|
10 |
<com:TTextHighlighter Language="prado" CssClass="source block-content" id="code_850268">
|
|
|
11 |
<com:TClientScript PradoScripts="effects" />
|
|
|
12 |
</com:TTextHighlighter>
|
|
|
13 |
</li>
|
|
|
14 |
<li>Adding libraries in PHP code
|
|
|
15 |
<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_850269">
|
|
|
16 |
$this->getPage()->getClientScript()->registerPradoScript("effects");
|
|
|
17 |
</com:TTextHighlighter>
|
|
|
18 |
</li>
|
|
|
19 |
</ul>
|
|
|
20 |
The available packaged libraries included in Prado are
|
|
|
21 |
<ul id="u2" class="block-content">
|
|
|
22 |
<li><tt>prado</tt> : basic PRADO javascript framework based on Prototype</li>
|
|
|
23 |
<li><tt>effects</tt> : visual effects from script.aculo.us</li>
|
|
|
24 |
<li><tt>ajax</tt> : ajax and callback related based on Prototype</li>
|
|
|
25 |
<li><tt>validator</tt> : validation</li>
|
|
|
26 |
<li><tt>logger</tt> : javascript logger and object browser</li>
|
|
|
27 |
<li><tt>datepicker</tt> : datepicker</li>
|
|
|
28 |
<li><tt>colorpicker</tt> : colorpicker</li>
|
|
|
29 |
</ul>
|
|
|
30 |
|
|
|
31 |
<p id="850761" class="block-content">The dependencies for each library are automatically resolved. Components
|
|
|
32 |
that require a particular library will also automatically load the necessary libraries.
|
|
|
33 |
For example, if you add a <tt>TDatePicker</tt> component on the page, the <tt>datepicker</tt>
|
|
|
34 |
and its dependencies will be automatically included on the page.</p>
|
|
|
35 |
|
|
|
36 |
<p id="850762" class="block-content">See <a href="?page=Controls.ClientScript">TClientScript</a> for options of adding
|
|
|
37 |
your custom Javascript code to the page.</p>
|
|
|
38 |
|
|
|
39 |
<h2 id="176028">Publishing Javascript Libraries as Assets</h2>
|
|
|
40 |
<com:SinceVersion Version="3.1b" />
|
|
|
41 |
<p class="block-content">Use <a href="?page=Controls.ClientScriptLoader">TClientScriptLoader</a> to publish and combine multiple existing javascript files (e.g. javascript libraries distributed with Prado or otherwise)
|
|
|
42 |
as packages.</p> For greater control on what and when to publish, use the
|
|
|
43 |
<tt>registerJavascriptPackages($base, $packages, $debug=null, $gzip=true)</tt>
|
|
|
44 |
method in the <tt>TClientScriptManager</tt> class, which an instance can be obtained
|
|
|
45 |
using <tt>$this->getPage()->getClientScript()</tt> or its equivalents.
|
|
|
46 |
For example, if multiple controls will use the same set of javascript libraries,
|
|
|
47 |
write a class to handle the registration of packages required by those controls.
|
|
|
48 |
</p>
|
|
|
49 |
<com:TTextHighlighter Language="php" CssClass="source block-content">
|
|
|
50 |
class MyJavascriptLib extends TComponent
|
|
|
51 |
{
|
|
|
52 |
private $_packages=array(); //keep track of all registrations
|
|
|
53 |
|
|
|
54 |
private $_manager;
|
|
|
55 |
|
|
|
56 |
protected function __construct(TPage $owner)
|
|
|
57 |
{
|
|
|
58 |
$this->_manager = $owner->getClientScript();
|
|
|
59 |
$owner->onPreRenderComplete = array($this, 'registerScriptLoader');
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
public static function registerPackage(TControl $control, $name)
|
|
|
63 |
{
|
|
|
64 |
static $instance;
|
|
|
65 |
if($instance===null)
|
|
|
66 |
$instance=new self($control->getPage());
|
|
|
67 |
$instance->_packages[$name]=true;
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
protected function registerScriptLoader()
|
|
|
71 |
{
|
|
|
72 |
$dir = dirname(__FILE__).'/myscripts'; //contains my javascript files
|
|
|
73 |
$scripts = array_keys($this->_packages);
|
|
|
74 |
$url = $this->_manager->registerJavascriptPackages($dir, $scripts);
|
|
|
75 |
$this->_manager->registerScriptFile($url,$url);
|
|
|
76 |
}
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
// example control class using the javascript packages
|
|
|
80 |
class TestComp extends TControl
|
|
|
81 |
{
|
|
|
82 |
public function onPreRender($param)
|
|
|
83 |
{
|
|
|
84 |
parent::onPreRender($param);
|
|
|
85 |
MyJavascriptLib::registerPackage($this,'package1');
|
|
|
86 |
}
|
|
|
87 |
}
|
|
|
88 |
</com:TTextHighlighter>
|
|
|
89 |
|
|
|
90 |
|
|
|
91 |
<div class="last-modified">$Id: Scripts3.page 1902 2007-05-07 04:17:37Z wei $</div></com:TContent>
|