How to use jQuery with a Yii. Map?

How to use jquery / javascript in yii ?,

How to use my script in yii?

Why is this no different than using jQuery in any other way?

+4
source share
3 answers

How to use jquery in yii?

As you said above, you can register a new script block or register a new external script file. You can also register assets in plugins that will process entire JS folders and merge them into your application.

Thus, there are many ways to use jQuery in Yii.

How to use my jquery in yii

JQuery comes bundled and activated using jQuery, and the jQuery library itself is considered the core of the script, so there is no need to embed your own jQuery.

You can overwrite inline jQuery with your own by setting:

$cs = Yii::app()->clientScript; $cs->scriptMap = array( 'jquery.js' => Yii::app()->request->baseUrl.'/js/jquery.js', 'jquery.yii.js' => Yii::app()->request->baseUrl.'/js/jquery.min.js', ); $cs->registerCoreScript('jquery'); $cs->registerCoreScript('jquery.ui'); 

Something like this is somewhere inside your layout, action or view. I prefer to put this in the layout and personally manage my own version of jQuery.

Why is this no different than using jQuery in any other way?

Mainly due to standardization and interoperability of the structure and its components. By pre-connecting with built-in hooks for the JQuery UI widget and JQuery elements, it is generally easy to build reusable and clean JS objects for use in your application.

Also, I like jQuery, so it makes sense that it appears in my way. However, this is not a call to arms, as JQuery is the only JS library / framework, and you should really think about what is best for you before you choose.

+11
source

using Yiis registerScript to load our jQuery, which when used usually looks like this:

 $(document).ready(function(){ alert('hello'); // Enter code here }); 

Now, if we use Yiis Script as follows:

 <?php Yii::app()->clientScript->registerScript('helloscript'," alert('hello'); ",CClientScript::POS_READY); ?> 

You will see that you need to pass 3 things to the Yii :: app () โ†’ clientScript-> registerScript function, firstly, the name of the script, which is not very important; it should just be unique from any other script name on the same page. LINK

+11
source

This is an example script that I used only in my development:

 <?php $js = " $('body') .on('click', '#cancelCreateInductionTemplate, #closeDialogBox', function (e) { // Close #openDialog e.preventDefault(); $('#openDialog').dialog('close'); }); "; Yii::app()->clientScript->registerScript('cancelCreateInductionTemplate', $js, CClientScript::POS_READY); ?> 
0
source

Source: https://habr.com/ru/post/1438597/


All Articles