Yii Framework Forum: Moving All Ajax and Java script code separately - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Moving All Ajax and Java script code separately Rate Topic: -----

#1 User is offline   Fan_Of_Yii 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 105
  • Joined: 28-July 11

Posted 13 April 2012 - 12:23 PM

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);
		} ',

),));?>

0

#2 User is offline   Haensel 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 441
  • Joined: 14-January 11
  • Location:Vienna (Austria)

Posted 13 April 2012 - 12:28 PM

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
0

#3 User is offline   wisp 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 192
  • Joined: 04-February 11

Posted 13 April 2012 - 01:12 PM

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..
0

#4 User is offline   Fan_Of_Yii 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 105
  • Joined: 28-July 11

Posted 13 April 2012 - 01:30 PM

View PostHaensel, on 13 April 2012 - 12:28 PM, said:

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


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
0

#5 User is offline   Fan_Of_Yii 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 105
  • Joined: 28-July 11

Posted 13 April 2012 - 01:37 PM

View Postwisp, on 13 April 2012 - 01:12 PM, said:

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 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
0

#6 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,099
  • Joined: 05-January 12
  • Location:Morocco

Posted 13 April 2012 - 01:43 PM

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:
  • you rewrite it
  • or you can see the js code that Yii generates, and migrate it (with adjustments I guess) to separate js files

0

#7 User is offline   Fan_Of_Yii 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 105
  • Joined: 28-July 11

Posted 14 April 2012 - 04:41 AM

View Postbennouna, on 13 April 2012 - 01:43 PM, said:

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:
  • you rewrite it
  • or you can see the js code that Yii generates, and migrate it (with adjustments I guess) to separate js files



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
0

#8 User is offline   Haensel 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 441
  • Joined: 14-January 11
  • Location:Vienna (Austria)

Posted 14 April 2012 - 05:36 AM

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/b...ript-from-html/
0

#9 User is offline   Fan_Of_Yii 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 105
  • Joined: 28-July 11

Posted 15 April 2012 - 08:38 AM

View PostHaensel, on 14 April 2012 - 05:36 AM, said:

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/b...ript-from-html/


Thank You Haensel for the link .

Regards
Yii Fan
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users