Rails JS best practices? (jQuery + Prototype / MooTools

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?

Thanks.

So, does anyone use PrototypeJS with jQuery?

On Mon, Jul 18, 2011 at 6:59 AM, Alexey P.
[email protected]wrote:

So, does anyone use PrototypeJS with jQuery?

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.

http://groups.google.com/group/rubyonrails-talk?hl=en.

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?

CoffeScript is standard from Rails 3.1 so it will be the best practice
for sure.
http://jashkenas.github.com/coffee-script/
Give it a try, bests:
gezope

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.

Walter

CoffeScript
I like CoffeeScript and already tried it :). But as it said before, it’s
supposed to be used with another libraries.

Hey, I’m the author of the CoffeeScript
bookhttp://pragprog.com/book/tbcoffee/coffeescript from
PragProg. I noticed this conversation and thought I’d stop by.

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).

On 19 Jul 2011, at 17:15, gezope wrote:

CoffeScript is standard from Rails 3.1 so it will be the best practice
for sure.
http://jashkenas.github.com/coffee-script/
Give it a try, bests:
gezope

On Jul 18, 12:59 am, Alexey P. [email protected] wrote:

So, does anyone use PrototypeJS with jQuery?

Best regards

Peter De Berdt

Here’s an interesting solution how to add all methods of underscore.js
to native types

// Underscore methods that we want to implement on Array.
var methods = ['each', 'map', 'reduce', 'reduceRight', 'detect',

‘select’,
‘reject’, ‘all’, ‘any’, ‘include’, ‘invoke’, ‘pluck’, ‘max’,
‘min’, ‘sortBy’,
‘sortedIndex’, ‘toArray’, ‘size’, ‘first’, ‘rest’, ‘last’,
‘without’,
‘indexOf’, ‘lastIndexOf’, ‘isEmpty’];

// Mix in each method as a proxy.
_.each(methods, function(method) {
  Array.prototype[method] = function() {
    return _[method].apply(_, [this].concat(_.toArray(arguments)));
  };
});

full thread is here Extend · Issue #38 · jashkenas/underscore · GitHub