Rails 3 upgrade: using Prototype + jQuery. Replace instances of '$' in rails.js with 'jQuery?'
We just upgraded to Rails 3.
We replaced the default "rails.js" file with the jQuery version.
Because our legac开发者_运维问答y app uses Prototype and its '$' reference, I assume we need to replace all '$' references with 'jQuery' in the jQuery version of "rails.js."
However, we have not done so, and everything seems to work fine. Moreover, we can't find documentation suggesting we need to.
Is this necessary? It's not clear how "rails.js" automatically knows to use jQuery instead of Prototype when it comes to '$' references.
Usually the way this is done is (as mentioned in a comment) like this:
(function($){
$(some_stuff_that_uses_$);
})(jQuery);
This is an anonymous self-executing function. You're basically taking an anonymous function function($){}
and then immediately running it by passing the parameter jQuery
to it. This is a quick way to make sure your jquery code stays separate from any other frameworks that might try to also use $
.
However, I think rails.js already does this...so it's very possible you won't have to do anything.
It's good practice to have all of your jquery files do this regardless of whether or not you have multiple frameworks.
精彩评论