So for a while now I have been using Prototype and jQuery together in my rails apps. I am kinda stuck with pPrototype, because it is fairly well embedded in rails. Which is fine with me because I don’t have to use it directly, just let rails render it. 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:
// Map over the $ in case of overwrite if ( typeof $ != “undefined” ) jQuery._$ = $;
// Map the jQuery namespace to the ‘$’ one var $ = jQuery;
// 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:
var $ = jQuery;
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 in_line_edit (which produces Prototype code) and jQuery in the same view, no problems.
If you wanted it to build a product you’d find a way to get time to work on it. If you really wanted to start that new hobby you’d sacrifice something to find the time and money to do it.
I'll define a "Wannabe Entrepreneur" as someone who has never made money from their businesses. Here are the different types of wannabes.
In the past few years I've built go-carts, built a 200+ sq ft workshop, written several eBooks. How do I create a life where I have time to work on side projects?
Receive 5 Software projects mistakes we have made over the years and how to avoid them.