what is the best form to load jquery?

Hi guys,

i would like know what is the best form to load jquery on my applications?

Thanks.

In every view that requires jquery you can put this on top:


<?php Yii::app()->clientScript->registerCoreScript('jquery'); ?>

Hi Mike,

can I put this on my layout?

If I use other jquery plugins, how can I load them?

Thanks.

Yes.

You can either use an extension that includes them (see repository) or manually put them into a web accessible folder and use


Yii::app()->clientScript->registerScriptFile('/url/of/plugin.js');



I guess I have same question.

If you put code to load every jquery extension you use in entire project in layout

it loads all of them in all pages, leading to bloat and possibly bad performance, you can mitigate with packing

but for me it’s cleaner to only load what you need for the page.

If you copy paste the load code only in views that need it you violate DRY (if you decide to use different extension

now you have to patch all views that use it.)

It seems like there should be some rules based way to do it in controller.

Anyone? Anyone? Bueller?

One solution might be to use a subview (resources.php) for including the script files




  $this->renderPartial('resources');



/Tommy

Hi tri,

I think that this is a wrong way to load files, becouse of the same

form you would be need include all jquery files in a layout, you will need include on this file, and call this file

when to be necessary.

I think that the first Mike’s interaction is the better way to do.

Thanks.

Hi, Jquery js,css all in yii framework files "framework\web\js\source\jui\js\"

I want to add "jquery-ui-i18n.min.js". This is exist in framework files.

How can i do this like this code ?


<?php Yii::app()->clientScript->registerCoreScript('jquery'); ?>

I would recommend that you copy the file

framework/web/js/source/jui/js/jquery-ui-i18n.min.js

to the "javascript/jui" directory under your document root -

/yourapplication/www/javascript/jui/jquery-ui-i18n.min.js

Then edit your controller, adding:





$baseUrl = Yii::app()->request->baseUrl; 

$clientScriptUrl = "{$baseUrl}/javascript/jui/jquery-ui-i18n.min.js"; 

$clientScript = Yii::app()->clientScript;

$clientScript->registerScriptFile($clientScriptUrl, CClientScript::POS_HEAD);






:)

Hey, tixrus, I find this is a very common situation here, and both your objections are right. I use this method: you know that sometimes you have to include an instruction DISCRIMINATELY in some modules of your application, and maybe only in some views of some of the modules. It is true, that it doesn’t make sense neither including the instruction for ALL the modules, and it doesn’t make sense to write it one-by-one in each of the several modules you need it. What I do, and suggest you to do, is to build a DIRECTORY that loads previously to the views. Such directory is a big SWITCH-CASE with your modules and the views in them. You will realize that this “instruction” that you discriminately want to introduce, is not the only situation in which you repeat code over and over in the several modules. For example, the pages titles for each module, when it says: “you have updated/created correctly the …[name of the database entity]”, can become handy when you want to customize such titles but the routine is so common that it doesn’t make sense to go through all the modules and views for that. The gii automatic feature is a great thing to start with and to make a quick module, but it contains a lot of repetitive code, that can be automatized by yourself within the particular framework for your application, and a useful method for doing that is with the big directory switch-case file I’m talking about.

Best regards from Bogotá, Colombia,

elexperimento

@elexperimento

Could you be so kind and post a simple example of your switch-case file and other related code which is needed so I and maybe some others are understanding your solution better

Thanks in advance