`

rails jquery and prototype

阅读更多

之前的项目用的是prototype  ,许多功能是用prototype实现的
现在项目要使用jquery,就必须混用
也可以只用jquery


There is a problem with using them together though: Prototype came up with this nice function called $() which is slightly more than a shortcut to document.getElementById(). This function is the main function in Prototype. Well,  John Resig , the creator of jQuery, also used to be a Prototype fan, so the main method in jQuery is also called $(). The jQuery website says that all you have to do to get them to play nice is to include jQuery after you include Prototype. Unfortunately, this is not enough. There is just a little bit more you have to do:

In the jQuery core code (which includes ajax, animations, et al.) there is a method called jQuery(). The $() method is aliased for the jQuery() in the code. This means that all you have to do is remove the part of jQuery that alias the $() method. In jQuery 1.0.4 uncompressed the lines you need to comment out start at line 60:

Change:

// Map over the $ in case of overwrite
if ( typeof $ != "undefined" )
jQuery._$ = $;

// Map the jQuery namespace to the '$' one
var $ = jQuery;

To:


// Map over the $ in case of overwrite
//if ( typeof $ != "undefined" )
//jQuery._$ = $;

// Map the jQuery namespace to the '$' one
//var $ = jQuery;

Once this is done, jQuery will no longer interfere with Prototype. You will have to use the jQuery() method to create a jQuery object, unless you alias the method to something else. I alias it to jq like this:

yessss

Change:

var $ = jQuery;

To:

var jq = jQuery;

One last thing:   Although all of the core uses JQuery() internally, there is no guaranty that any plugin will do the same, in my experience none do. The solution that works for me is to do a find/replace in the downloaded plugin, replacing any $( with jQuery(.

That's it. Now you can easily use jQuery and prototype in your rails app, even in the same view. I regularly use inline edit (which produces Prototype code) and jQuery in the same view, no problems.

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics