jQuery plugins in YII_DEBUG=false are not working

Hi,

I recently ran into the following problem:

I got a custom JS-File called index.js. In this file I put my jQuery plugins/extensions like a cookie Plugin or a highlightning Plugin.

With YII_DEBUG=true everythig is doing fine, but when I turn off YII_DEBUG (for production) the jQuery Plugins in the index.js are not regognized anymore, while the index.js itself is loaded from the client.

So I ran into several "missing function" errors.

My first hint went into the fact, that instead of jquery.js jquery.min.js gets loaded in production-environment. But as the jquery.min.js has the same functionality as the jquery.js I would not expect any problems.

To be sure I used scriptMap like


	'components'=>array(

		'clientScript'=>array(

			'scriptMap'=>array(

         	'jquery.min.js'=>'https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.js',

          ),

		),

just to prove, that the problem did not depend on the jquery.whatever file at all, because the error still occurs.

So, does anybody has an idea on how YII differs in the use of jQuery and jQuery Plugins between YII_DEBUG true and false?

Best regards

Ralf

Should be no difference except minified version is used.

Yes, thats what I thought also, but it seems to make a difference.

To point out what I did to examine the problem:

When YII_DEBUG is set to false, jQuery extensions seems not to be loaded. The extensions/plugins resides in a separated index.js file, which is loaded by the client as the jquery.min.js also.

When using those jQuery extensions, FireBug reports "missing function …".

That only occurs in YII_DEBUG = false. With YII_DEBUG=true everything is going well.

I did 3 things:

  1. Used scriptmap as mentioned above and verified it with FB. Looked like the jquery.js from google was loaded. But the "missing function …" error was still there.

  2. Used scriptmap to load the jquery.js in my asset-folder. I know, thats something we shouldn’t do, but for debugging its ok, I guess. Nevertheless, it drops the same error-message as mentioned in 1.

  3. Modified /web/js/packages.php and changed the ‘jquery.min.js’ in the first package in ‘jquery.js’, so that independent of YII_DEBUG on or off it’s always loads the jquery.js. Well, that solves the “missing function…” problem.

I know, that is dirty and not wanted under any circumstances.

Maybe important:

The jquery.js and jquery.min.js (depending on the YII_DEBUG state) are always loaded before my custom index.js.

For me it looks like YII handles jQuery-extensions in a different way when in DEBUG=false.

I would like to have some advice if it makes sense to open a bug-report.

I will try to do a testcase on a fresh application with nothing but a startpage and some JavaScript.

Best regards

Ralf

It’s strange:

I set up a minimal environment (just the demo-app) and put in some JavaScript, a jQuery-plugin an some onclick-Events for testing-purposes.

They did nothing more then setting a cookie, for example.

To make a long story short: Its doing fine, in YII_DEBUG, false either in YII_DEBUG, true.

So the project, where the mentioned problem occurs, is much,much more complex. I’ve no idea where to start or how I can debug this problem.

Maybe someone can come around with suggestions?

Best regards

Ralf

I don’t know if loading jquery.js followed by jquery.min.js would be worse than just loading jquery.js twice, but it could be worth it checking for multiple inclusions.

/Tommy

Oh, missunderstanding :)

There is only jquery.js or jquery.min.js loaded at a time, depending on YII_Debug true or false - not both at the same time.

Ralf

Try removing all your customizations: script map, loading from assets etc. Then check if it works.

Not a misunderstanding. Just one possible possibility to keep in mind since you told us

and also

/Tommy

Good morning,

@samdark: Indeed there was another scriptMap besides the one in the config/main.php, regarding to the jquery.js. I removed that one temporarily as the one in the main.php. Unfortunaly without success.

@tri: It’ like the following:

YII_DEBUG = true

First, the jquery.js gets loaded, then our customized index.js with the jquery-extensions.

No jquery.min.js is loaded.

YII_DEBUG = false

First, the jquery.min.js will be served, then - no surprise :slight_smile: - our index.js.

No jquery.js is loaded.

There is an jQuery-Extension in our index.js called “cookie”, which is called with $.cookie(‘value’).

In YII_DEBUG = true mode $.cookie is working. With YII_DEBUG = false it isn’t.

But: All the other non-jQuery related functions in our index.js are doing fine.

I messed around with a little bit of positioning of the index.js script, like CClientScript::POS_END, but it doesn’t work neither.

Ideas are very welcome :slight_smile:

Ralf

Looks, like I found the solution.

First, thanks @samdark, pushing me the right way :slight_smile:

As mentioned in my last post, we have a scriptMap in one of our Controller/Actions.

It was:




Yii::app()->clientScript->scriptMap=array('jquery.js'=>false,);



My first shot was commenting that one out/ remove it, but without any success (as I told half an hour ago).

But I tried something else, which is doing the magic:




Yii::app()->clientScript->scriptMap=array((YII_DEBUG ? 'jquery.js' : 'jquery.min.js')=>false,);



I am confused why commenting it out doen’t help but the second approach is working.

But for now it seems to be fine.

Best regards

Ralf

This looks like you had a third combination: jquery.min.js loaded twice.

In my previous post, I mentioned two combinations, assuming you loaded jquery.js somewhere else

(I don’t know all side effects of loading jQuery twice, that’s why I wrote like I did.)

/Tommy