[SOLVED] Yii Render Removes jQuery Bindings?

Hey all,

I have an action that calls render() to render an initial page view. This page loads several jQuery plugins and also uses ajaxStart/ajaxStop to show a ‘loading’ div when an ajax call is fired off. There are several buttons on the page that make ajax calls to load variable content into a #panel div.

The first click of any of these buttons fires off the ‘loading’ div and gets the new content. Any subsequent click of any of the buttons gets the new content but does not fire the ajaxStart event and so the ‘loading’ div does not display. A page refresh to the initial render()'d page restores the ‘loading’ functionality but again only for the first click.

The JS file that contains the ajaxStart and ajaxStop code is loaded into the initial page render(), and it’s related div is outside of #panel, so why are the bindings getting hosed when I call Yii’s renderPartial? The partial does not contain any elements that have bindings that require live() or listen() so should be completely separate from the javascript and elements that are loaded on initial render().

My guess is that because I have renderPartial set to processOutput (there’s a CStarRating that needs to be handled), that renderPartial is wiping out the previous javascript by resending all core javascript files to the client because of the widget in the response.

This causes another issue as well. If the javascript gets resent (verified by firebug), the number of ajax requests doubles with each click of an ajax-enabled element because each time it’s called, it adds yet another version of the javascript to the final page.

How can I get renderPartial to leave the initial javascript alone but still have it parse the widget jQuery in the partial? Specially since the initial javascript doesn’t do anything with the elements in the partial?

Check if this will help

Yii::app()->clientScript->scriptMap['selectedscript.js'] = false;


Yes, indeed. Mapped out jquery itself and everything works perfectly. Thanks man :)

I had the same problem in a view that I render in an ajax-loaded tab (CJuiTabs) in a CJuiDialog. My view contains a dropdownlist that uses an ajax request to the server. The dropdownlist loaded new js files every time the view was refreshed i.e. every time you click on the tab. This multiple js files caused multiple submissions.

I tried this: Yii::app()->clientScript->scriptMap[‘jquery.min.js’] = false;

It prevented loading extra js files when the view was refreshed, but after the dialog was closed, the js files was still loaded in the browser. When the dialog was re-opened and the tab clicked, extra js files were again loaded, causing multiple submissions again.

I end up removing all ajax from the dropdownlist and put it in a custom js function, which I called using the dropdownlist’s ‘onchange’ event. This stopped all multiple submissions, even if multiple js files were loaded in the browser.

Check this wiki