0

I use Backbone router to make ajax call to Yii action which in turn loads partial view

this->renderPartial('_foo',null,false,true);

But the problem occurs as I use the same ajax call multiple times, it loads all scripts from partial view again and again, which in turn breaks the app.

Is there a way to fix this using the same action call pattern? Or if not how should I make Yii and Backbone cooperate?

user1750290
  • 125
  • 9
  • do you need processOutput(4th param to renderPartial) to be true? – bool.dev Dec 13 '12 at 10:18
  • If it is false then it ignores registerScript all together – user1750290 Dec 13 '12 at 11:08
  • meaning you need a few scripts, but not all scripts? can you show the list? – bool.dev Dec 13 '12 at 11:10
  • take a look [here](http://stackoverflow.com/a/10188538/720508) – bool.dev Dec 13 '12 at 11:24
  • It is basically the problem, but I want to stop scripts from yii widget which you cannot easily match because they have numbered format like so assets/68368b44/jquery.js/eval/seq/9, and after loaded they seem to be deleted together with jquery.js dir – user1750290 Dec 13 '12 at 11:49
  • try and edit your question to make the problem more specific, add the necessary details, someone should be able to help. – bool.dev Dec 13 '12 at 11:59

1 Answers1

0

Yii widgets are very poorly designed when it comes to working with ajax calls that have elements that require additional scripts to be loaded.

The solution posted by bool.dev in the comments is my preferred way of dealing with this issue, but there are others as well, if that does not work for whatever reason (which appears to be the case for you).

In your primary view file (the one you call with render(), not partialRender()), including the following:

$this->renderPartial('_foo',null,true,true);

This will force your main page call (so, before the ajax calls) to include the scripts. Since the 3rd parameter is now true, it will return the contents as a string, which then gets discarded because we are not storing it anywhere.

Then for the ajax calls, use

$this->renderPartial('_foo',null,false,false);

or just

$this->renderPartial('_foo');

since the last 3 parameters are just the defaults.

This will prevent the scripts from being loaded multiple times, but still be present, since they are there on the original page load but not the ajax calls.

Depending on what is in your _foo view file you may need to make a special file. All that is required is the a dummy usage of the widget you want the script files from.

Remember, all you are trying to do is force the widget to run on the main page call so it registers its scripts, and then discard the remaining output. Then on ajax calls ignore all the scripts it registers by setting the processOutput parameter to false.

Willem Renzema
  • 5,177
  • 1
  • 17
  • 24