Hi, is there any ‘best practice’ on using JS libraries with Rails?
Currently the default library is jQuery, and it has a cool DOM API, but
the core language API looks not very good:
$(array).each ...
Lately there’s come the ‘underscore’ library from J.Ashkenas it helps a
little, but it still looks the same:
_(array).each ...
I prefer the prototype way, like this:
array.each …
How do You solve this? Do You use it as it is or use some workaround by
including part of Prototype or MooTools with jQuery.
Or it just doesn’t worth it and it’s better to just use as it is?
I’ve used prototype with jquery before. Now, I only use jquery since
it’s
supported out of the box
by rails3 (with ajax helpers working using the jquery-rails gem). I
remember that you just have to
add a single line to make prototype working with jquery.
var $j = jQuery.noConflict();
then remember to use $j instead of $ when you want to use jquery
functions.
Hmm, I mean to integrate it a little differently, without using jQuery
safe mode.
Usually You also needs a lots of plugins that works with jQuery, and
some of them probably will not work in safe mode.
There should be no conflict and functional duplication, I suggested to
include only the ‘lang.js’ part of Prototype (without it’s DOM and AJAX
parts). Take a look please at the link below, it seems that it’s
possible to include only lang.js or do I miss something?
It’s generally not a good idea to use two large libraries like this in
the same page. Not only do they conflict (unless you use the
jQuery.noConflict() patch to rewrite any uses of the $ function
defined in Prototype) but they each do more or less the same thing, so
it’s a waste to force a download of both on your users.
It’s been my experience that anything you want to do in jQuery you can
do in Prototype, and vice-versa.
CoffeeScript goes great with either Prototype or jQuery. (One chapter of
my
book is a primer on jQuery.) As Walter said, while it’s possible to use
both
Prototype and jQuery on the same web page, it’s generally not very
efficient
because both libraries offer much of the same functionality. Prototype
has
traditionally had a large following among Rubyists because it provides
lots
of Ruby-like idioms. But jQuery is much more widely used and actively
updated than Prototype, and is the official JS library as of Rails
3.1even
though the creator of Prototype, Sam S., works for 37signals.
(He’s
also become a strong advocate of CoffeeScript and was a technical
reviewer
for the book.)
So, in short, I’d suggest sticking with CoffeeScript + jQuery. If you
miss
Rails’ functional programming goodness, check out
Underscore.jshttp://documentcloud.github.com/underscore/.
And if you feel like modifying native prototypes (e.g. adding an
uppercasemethod to all strings), feel free. Check out how Prototype does
it:
If you have any questions, you can always find me on Stack Overflow.
Coffeescript has nothing to do with Javascript libraries like jQuery
or Prototype. It’s a precompiler for Javascript, i.e. it converts
Coffeescript code to Javascript.
Yes, we use Prototype and jQuery together. You just need to put jQuery
into compatibility mode and then use jQuery(“blahblah”) for jQuery
code and the usual $ for Prototype. Be on the lookout for badly
written jQuery plugins though (the ones that don’t scope their code).
// Mix in each method as a proxy.
_.each(methods, function(method) {
Array.prototype[method] = function() {
return _[method].apply(_, [this].concat(_.toArray(arguments)));
};
});