Moving All Ajax and Java script code separately

Dear All,

In my development I put all the ajax code inside my CHtml tags. I have given example below , Like this I have lot of Java script, Jquery and ajax calls in the code . I don’t find a way to separate them now … . Do we have to separate that code to separate java script files to speed up the application ? I read it in our forums somewhere there is a way to separate them … Could you please throw some light on this? Thanks for your help


<?php echo CHtml::dropDownList('nat_type',$per_type,$per_types,

array(

	'ajax' => array(

	'type' => 'POST',

	'beforeSend' => 'function(){$("#myDiv").addClass("loading");}',

	'complete' => 'function(){$("#myDiv").removeClass("loading");}',

	'dataType'=>'json',

	'url' => CController::createUrl('myMasterTypes/types'),

	'success'=>'function(data) {

		$("#car").html(data.dropDownA);

		$("#car").prepend(\'<option value="" selected> - Select Car - </option>\');                                                                                                

		refreshList(1);

	}',

	'error'=>'function (xhr, ajaxOptions, thrownError){

						alert(xhr.responseText);

		} ',


),));?>

You can use CClientScript to register javascript code via registerScript() or registerScriptFile() if you want to separate it away into files: Take a look at the docs here CClientScript

You can also use the controller/method name to construct the filenames and include them automatically…

So site/index would include site/index.js

That way everything is automatically organised…

Thanks for your response Haensel . Yes I used this to include external script and my JSP files. But to separate the already written ajax code automatically ? I mean already code is written ,do I have to manually separate and write JS files and register them …?

Thanks for your help

Regards

Yii Fan

Thanks for your response wisp. I was not knowing this . This definitely helps .Could you please let me know where I have to copy the "site/index.js " files ? I mean controller/method name JS files full path …

I know this is too much to ask , is there any way to separate the already written ajax code automatically ? I mean already code is written ,do I have to manually separate and write JS files …?

Again for the below example code , Do I have to manually write corresponding AJAX JS code ( To remove the ajax code from here ) and move it to the JS files ? Any easy way to separate them ?


<?php echo CHtml::dropDownList('nat_type',$per_type,$per_types,

array(

        'ajax' => array(

        'type' => 'POST',

        'beforeSend' => 'function(){$("#myDiv").addClass("loading");}',

        'complete' => 'function(){$("#myDiv").removeClass("loading");}',

        'dataType'=>'json',

        'url' => CController::createUrl('myMasterTypes/types'),

        'success'=>'function(data) {

                $("#car").html(data.dropDownA);

                $("#car").prepend(\'<option value="" selected> - Select Car - </option>\');                                                                                                

                refreshList(1);

        }',

        'error'=>'function (xhr, ajaxOptions, thrownError){

                                                alert(xhr.responseText);

                } ',


),));?>



Thanks for your help

Regards

Yii Fan

Thanks wisp for the tip, I didn’t know that.

@yadino: if you want to separate the code, you have 2 ways of doing that:

[list=1]

[*]you rewrite it

[*]or you can see the js code that Yii generates, and migrate it (with adjustments I guess) to separate js files

[/list]

Thank You bennouna for your reply . Yes , I am doing view source of the page and copying all the java script in separate files . Not sure this is the best way to do it .

Thanks again

Regards

Yii Fan

There is a really nice article by Weavora on how to separate HTML and Javascript using Yii (although this is applicable for all MVC style frameworks): http://weavora.com/blog/2011/11/24/yii-framework-an-easy-was-to-split-javascript-from-html/

Thank You Haensel for the link .

Regards

Yii Fan