| 1 |
lars |
1 |
<com:TContent ID="body" >
|
|
|
2 |
|
|
|
3 |
<h1>TClientScriptLoader</h1>
|
|
|
4 |
|
|
|
5 |
<com:DocLink ClassPath="System.Web.UI.WebControls.TClientScriptLoader" />
|
|
|
6 |
|
|
|
7 |
<com:SinceVersion Version="3.1b" />
|
|
|
8 |
|
|
|
9 |
<p class="block-content">
|
|
|
10 |
The <tt>TClientScriptLoader</tt> publish a collection of javascript files as assets.
|
|
|
11 |
For example, suppose we have a directory name "<tt>mylib</tt>" in
|
|
|
12 |
the <tt>protected/pages</tt> directory of our application.</p>
|
|
|
13 |
<com:TTextHighlighter Language="prado" CssClass="source block-content">
|
|
|
14 |
assets/
|
|
|
15 |
protected/
|
|
|
16 |
pages/
|
|
|
17 |
mylib/
|
|
|
18 |
file1.js
|
|
|
19 |
file2.js
|
|
|
20 |
file3.js
|
|
|
21 |
file4.js
|
|
|
22 |
packages.php
|
|
|
23 |
</com:TTextHighlighter>
|
|
|
24 |
|
|
|
25 |
<p class="block-content">The <tt>PackagePath</tt> property can be an existing asset directory
|
|
|
26 |
or a namespace path to the directory containing javascript files.
|
|
|
27 |
For example, to publish the javascript files in the <tt>mylib</tt>
|
|
|
28 |
directory, we can specify the <tt>PackagePath</tt> as follows.
|
|
|
29 |
The first tag <tt>TClientScriptLoader</tt> relies on the asset template tag and
|
|
|
30 |
assumes that the page template containing the <tt>TClientScriptLoader</tt> tag instance
|
|
|
31 |
is in the <tt>protected/pages</tt> directory.
|
|
|
32 |
The second <tt>TClientScriptLoader</tt> tag uses the namespace notation to
|
|
|
33 |
specify the path.
|
|
|
34 |
</p>
|
|
|
35 |
|
|
|
36 |
<com:TTextHighlighter Language="prado" CssClass="source block-content">
|
|
|
37 |
<com:TClientScriptLoader PackagePath=<%~ mylib %> />
|
|
|
38 |
<com:TClientScriptLoader PackagePath="Application.pages.mylib" />
|
|
|
39 |
</com:TTextHighlighter>
|
|
|
40 |
|
|
|
41 |
<p class="block-content">
|
|
|
42 |
When the files in the <tt>PackagePath</tt> are published as assets, a script loader
|
|
|
43 |
php file "<tt>clientscripts.php</tt>" is automatically copied
|
|
|
44 |
to that asset directory.
|
|
|
45 |
The script loader, combines multiple javascript files and serve up as gzip if possible.
|
|
|
46 |
</p>
|
|
|
47 |
<h2>Grouping Javascript Files</h2>
|
|
|
48 |
<p class="block-content">
|
|
|
49 |
Allowable scripts and script dependencies can be grouped by using a "<tt>packages.php</tt>" file
|
|
|
50 |
with the following format. This "<tt>packages.php</tt>" is optional, if absent the file names
|
|
|
51 |
without ".js" extension are used. The "<tt>packages.php</tt>" must be in the directory given by
|
|
|
52 |
<tt>PackagePath</tt>.
|
|
|
53 |
</p>
|
|
|
54 |
|
|
|
55 |
<com:TTextHighlighter Language="php" CssClass="source block-content">
|
|
|
56 |
<?php
|
|
|
57 |
$packages = array(
|
|
|
58 |
'package1' => array('file1.js', 'file2.js'),
|
|
|
59 |
'package2' => array('file3.js', 'file4.js'));
|
|
|
60 |
|
|
|
61 |
$deps = array(
|
|
|
62 |
'package1' => array('package1'),
|
|
|
63 |
'package2' => array('package1', 'package2')); //package2 requires package1 first.
|
|
|
64 |
|
|
|
65 |
return array($packages,$deps); //must return $packages and $deps in an array
|
|
|
66 |
?>
|
|
|
67 |
</com:TTextHighlighter>
|
|
|
68 |
|
|
|
69 |
<p class="block-content">The first element of the array returned by the <tt>packages.php</tt> should
|
|
|
70 |
contain an array of javascripts files relative to the <tt>packages.php</tt>
|
|
|
71 |
that corresponds to a particular grouping. For example, in the above <tt>packages.php</tt>,
|
|
|
72 |
the grouping '<tt>package1</tt>' combines two javascript files, namely, '<tt>file1.js</tt>'
|
|
|
73 |
and '<tt>file2.js</tt>'.
|
|
|
74 |
</p>
|
|
|
75 |
|
|
|
76 |
<p class="block-content">The second element of the array returned by the <tt>packages.php</tt> should
|
|
|
77 |
contain an array of grouping dependencies ordered in the way that the groups should be combined.
|
|
|
78 |
For example, grouping '<tt>package1</tt>' only depends on the '<tt>package1</tt>' grouping files
|
|
|
79 |
(i.e. '<tt>file1.js</tt>' and '<tt>file2.js</tt>'). While '<tt>package2</tt>' depends
|
|
|
80 |
on both '<tt>package1</tt>' and '<tt>package2</tt>' groupings. That is, '<tt>package2</tt>'
|
|
|
81 |
will combine, in order, '<tt>file1.js</tt>', '<tt>file2.js</tt>', '<tt>file3.js</tt>', and '<tt>file4.js</tt>'.
|
|
|
82 |
</p>
|
|
|
83 |
|
|
|
84 |
<h2>Loading Javascript Packages</h2>
|
|
|
85 |
|
|
|
86 |
<p class="block-content">To load a particular javascript file or package, set the <tt>PackageScripts</tt>
|
|
|
87 |
property with value '<tt>package1</tt>' to load the '<tt>package1</tt>' scripts.
|
|
|
88 |
A maximum of 25 packages separated by commas is allowed.
|
|
|
89 |
Dependencies of the packages are automatically resolved by the script loader php file.
|
|
|
90 |
</p>
|
|
|
91 |
|
|
|
92 |
<com:TTextHighlighter Language="prado" CssClass="source block-content">
|
|
|
93 |
<com:TClientScriptLoader PackagePath=<%~ mylib %> PackageScripts="package2" />
|
|
|
94 |
<script type="text/javascript">
|
|
|
95 |
//javascript code utilizing javascript code loaded in 'package2' above
|
|
|
96 |
</script>
|
|
|
97 |
</com:TTextHighlighter>
|
|
|
98 |
|
|
|
99 |
<p>Each <tt><com:TClientScriptLoader></tt> generates an HTML <tt><script></tt>
|
|
|
100 |
element to load the required javascript files.</p>
|
|
|
101 |
|
|
|
102 |
<h2>Removing Javascript Comments</h2>
|
|
|
103 |
<p class="block-content">The <tt>DebugMode</tt> property when false
|
|
|
104 |
removes comments and white spaces from the published javascript files. If
|
|
|
105 |
the <tt>DebugMode</tt> property is not set, the debug mode is determined from the
|
|
|
106 |
<a href="?page=Advanced.Performance">application mode</a>.
|
|
|
107 |
</p>
|
|
|
108 |
<div class="note"><b class="note">Note:</b>
|
|
|
109 |
If the <tt>DebugMode</tt> is false either explicitly or when the application mode is non-debug,
|
|
|
110 |
then cache headers are also sent to inform the browser and proxies to cache the file.
|
|
|
111 |
Moreover, the post-processed (comments removed and zipped) are saved in the assets
|
|
|
112 |
directory for the next requests. That is, in non-debug mode the scripts are cached
|
|
|
113 |
in the assets directory until they are deleted.
|
|
|
114 |
</div>
|
|
|
115 |
|
|
|
116 |
<h2>Compressing Javascript with GZip</h2>
|
|
|
117 |
<p class="block-content">
|
|
|
118 |
The <tt>EnableGzip</tt> property (default is true) enables the
|
|
|
119 |
published javascripts to be served as zipped if the browser and php server allows it.
|
|
|
120 |
</p>
|
|
|
121 |
<div class="last-modified">$Id: ClientScript.page 1650 2007-01-24 06:55:32Z wei $</div>
|
|
|
122 |
|
|
|
123 |
</com:TContent>
|