Pages

Saturday, June 4, 2011

[yii] When yii fucks with you, loading jquery twice.

Ahhhh the joys of the yii framework.
I admit it, it is frakking fast and cool and all that, but sometimes you just encounter weird shit. Yeah, weird shit.
For example:
Imagine you have a sweet jquery UI modal dialog, that is opened by a link. It works flawlessly until you click another ajax thingy and the whole thing doesn't work anymore... WTF?
Ok, you might check if you are executing a renderPartial which contains an ajax object...  like a CHtml::ajaxSubmitButton, or a CHtml::ajaxLink, or any fucking ajax thing... via ajax..
Check the developerconsole (of chrome) and keep your eyes open if you see something like this

Uncaught TypeError: Object [object Object] has no method 'dialog' <- this is the Error you will probably get, desperately clicking on a damn button which won't open your dialog.

Then, reload the page and clear the Network-Output.... then click on the part that breaks things. If you see something like this:

Jackpot. The jquery.js gets loaded a second time and pushed back via the html output.

Yii somehow thinks it might be a great idea to insert a reference to the jquery.js even when you just render a partial. uh, great... so you have 2 jquery.js loaded. Bad. Bad bad bad.

If you want to prevent that a jquery reference is generated, add following code to your ajax-action in your controller:

Yii::app()->clientScript->scriptMap=array('jquery.js'=>false,);
It prevents that the jquery-script is being loaded.

If that's not the solution, your jquery might not be initialized at all, but that's another story.

cheers.

5 comments:

SCORPION said...

Thanks a lot man!!

Алексей said...

Thanks a lott!!!

JCSM said...

I had to include the following for mine to work:

'jquery.min.js' => false

thanks!

Labinskiy Nikolay said...

Thank you!

adipurwo said...

Thank you very much from Indonesian yii developer :D

Post a Comment