20.11.2014 Views

O Guia Definitivo do Yii 1.1

O Guia Definitivo do Yii 1.1

O Guia Definitivo do Yii 1.1

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Note: The scriptMap feature described in the following has been available since<br />

version 1.0.3.<br />

First, we declare the scripts to be minimized by configuring the scriptMap property of the<br />

clientScript application component. This can be <strong>do</strong>ne either in the application configuration<br />

or in code. For example,<br />

$cs=<strong>Yii</strong>::app()->clientScript;<br />

$cs->scriptMap=array(<br />

'jquery.js'=>'/js/all.js',<br />

'jquery.ajaxqueue.js'=>'/js/all.js',<br />

'jquery.metadata.js'=>'/js/all.js',<br />

......<br />

);<br />

What the above code <strong>do</strong>es is that it maps those JavaScript files to the URL /js/all.js. If any<br />

of these JavaScript files need to be included by some components, <strong>Yii</strong> will include the<br />

URL (once) instead of the individual script files.<br />

Second, we need to use some tools to merge (and perhaps compress) the JavaScript files<br />

into a single one and save it as js/all.js.<br />

The same trick also applies to CSS files.<br />

We can also improve page loading speed with the help of Google AJAX Libraries API. For<br />

example, we can include jquery.js from Google servers instead of our own server. To <strong>do</strong><br />

so, we first configure the scriptMap as follows,<br />

$cs=<strong>Yii</strong>::app()->clientScript;<br />

$cs->scriptMap=array(<br />

'jquery.js'=>false,<br />

'jquery.ajaxqueue.js'=>false,<br />

'jquery.metadata.js'=>false,<br />

......<br />

);<br />

By mapping these script files to false, we prevent <strong>Yii</strong> from generating the code to include<br />

these files. Instead, we write the following code in our pages to explicitly include the script<br />

files from Google,

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!