Re: Rails JS best practices? (jQuery + Prototype / MooTools

20 views
Skip to first unread message

Alexey Petrushin

unread,
Jul 17, 2011, 6:59:51 PM7/17/11
to rubyonra...@googlegroups.com
So, does anyone use PrototypeJS with jQuery?

--
Posted via http://www.ruby-forum.com/.

Jim Ruther Nill

unread,
Jul 17, 2011, 11:37:53 PM7/17/11
to rubyonra...@googlegroups.com
On Mon, Jul 18, 2011 at 6:59 AM, Alexey Petrushin <li...@ruby-forum.com> 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.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.




--
-------------------------------------------------------------
visit my blog at http://jimlabs.heroku.com

Walter Lee Davis

unread,
Jul 18, 2011, 4:04:12 PM7/18/11
to rubyonra...@googlegroups.com
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

> --
> You received this message because you are subscribed to the Google
> Groups "Ruby on Rails: Talk" group.

> To post to this group, send email to rubyonrails-
> ta...@googlegroups.com.

Alexey Petrushin

unread,
Jul 18, 2011, 7:23:28 PM7/18/11
to rubyonra...@googlegroups.com
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 I'm miss something?

https://github.com/sstephenson/prototype/tree/master/src/prototype

gezope

unread,
Jul 19, 2011, 11:15:12 AM7/19/11
to Ruby on Rails: Talk
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

Peter De Berdt

unread,
Jul 19, 2011, 11:56:10 AM7/19/11
to rubyonra...@googlegroups.com
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 Petrushin <li...@ruby-forum.com> wrote:
So, does anyone use PrototypeJS with jQuery?




Best regards


Peter De Berdt


Alexey Petrushin

unread,
Jul 19, 2011, 1:54:02 PM7/19/11
to rubyonra...@googlegroups.com
> CoffeScript
I like CoffeeScript and already tried it :). But as it said before, it's
supposed to be used with another libraries.

Trevor Burnham

unread,
Jul 19, 2011, 6:09:21 PM7/19/11
to rubyonra...@googlegroups.com
Hey, I'm the author of the CoffeeScript book 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.1—even though the creator of Prototype, Sam Stephenson, 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.js. And if you feel like modifying native prototypes (e.g. adding an uppercase method to all strings), feel free. Check out how Prototype does it: https://github.com/sstephenson/prototype/tree/master/src/prototype/lang

If you have any questions, you can always find me on Stack Overflow.

Alexey Petrushin

unread,
Jul 22, 2011, 1:13:28 PM7/22/11
to rubyonra...@googlegroups.com
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 http://github.com/documentcloud/underscore/issues/38

Alexey Petrushin

unread,
Jul 24, 2011, 12:11:50 PM7/24/11
to rubyonra...@googlegroups.com
One more interesting (and active!) project, see screenshots on their
site https://github.com/Mon-Ouie/ray

I recently discovered another interesting option - integration with game
via socket. For example, this project https://github.com/d-snp/RProxyBot
(ruby driver for StarCraft 1 from AI Bots competition) uses socket to
connect to the StarCraft 1 game and control all the game.

Would be nice to have such thing for games like this:

FLARE http://clintbellanger.net/rpg/blog/20110709 (diablo clone, with
very cool graphics)
Glest http://glest.org (warcraft 3 clone)

FLARE is especially interesting because it has very high quality art and
small and clean source code size.

Reply all
Reply to author
Forward
0 new messages