15

Wondering if anyone here can offer any insight into the ups/downs of using the RightJS library, specifically as compared to jQuery, and generally compared to what you think a library ought to offer.

I'm not so much looking for a feature to feature comparison, but rather a sense of the general daily use.

Things like:

  • Does it feel natural to use, or does it feel like an uphill battle?
  • Does the API express itself in a comprehensible manner, or do you find yourself wondering what the code you wrote 3 weeks ago means?
  • Do you find yourself wishing it had some feature of jQuery (or some feature in general), or inversely, do you enjoy some particular feature that other libraries don't have?
  • General philosophical considerations that you think make RightJS superior/inferior.

Not things like:

  • Mindshare/marketshare/plugins/CDN/ considerations (the winner is obvious)
  • Why would you bother... (moot)
  • jQuery does x, y and z, and RightJS doesn't, without offering insight into how it impacts daily use (there could be philosophical reasons that make x, y and z unnecessary)
user113716
  • 318,772
  • 63
  • 451
  • 440

4 Answers4

14

Based on the documentation, code samples, and what is already in the works for RightJS 2, I am very impressed.

@Patrick - Thanks for pointing out the Call By Name feature in RightJS which seems extremely useful for removing most anonymous functions from all over the page. For those who are not familiar with it, the idea is to specify the method name and arguments as parameters instead of creating an anonymous callback. For example, if we are trying to filter all words and phrased that begin with "hello" from an array,

var words = ['stack', 'hello world', 'overflow', 'hello there'];

Using the filter method on arrays, we would write:

words.filter(function(word) {
    return word.indexOf('hello') === 0;
});

We can write the same thing using Call By Name in RightJS as,

words.filter('startsWith', 'hello');

Pretty sweet eh?

I am also loving the idea of being able to use strings as selectors directly. Although RightJS only does it for event delegation at the moment, but I would love to be able to completely get rid of the $ function for most purposes and directly call methods on a string. For example, to listen to all clicks on any para on the page, write:

'p'.on('click', function() {
    this.addClass('clicked');
});

Or maybe combine this with Call By Name,

'p'.on('click', 'addClass', 'clicked');

The things I am excited about in RightJS 2 is the ability to use widgets as native elements.

var calendar = new Calendar();

calendar instanceof Calendar; // true
calendar instanceof Element; // true

Thanks to @patrick and @Nikolay for clarifying my assumption here. The widget will wrap the native DOM element which is available as the _ property on the object.

document.body.appendChild(calendar._);

or use the methods provided by the framework.

calendar.insertTo(document.body)

Another nice feature is a unified way to initialize widgets using just a declarative syntax:

<input data-calendar="{format: 'US', hideOnClick: true}" />

instead of creating an object ourselves and then adding it to the page:

var calendar = new Calendar({
    format: 'US',
    hideOnClick: true
});

calendar.insertTo(document.body);

I have taken the slides from a presentation titled JavaScript Library Overview by John Resig and compared the code samples for jQuery with RightJS. These samples mainly compare the basic syntax for both frameworks which is more similar than different, although RightJS seems to be more flexible in its usage.

DOM Traversal

jQuery

$('div > p:nth-child(odd)')

RightJS

$$('div > p:nth-child(odd)')

DOM Modification

jQuery

$('#list').append('<li>An Item</li>')

RightJS

$('list').insert('<li>An Item</li>')

Events

jQuery

$('div').click(function() {
    alert('div clicked')'
});

RightJS

$$('div').onClick(function() {
    alert('div clicked');
});

AJAX

jQuery

$.get('test.html', function(html) {
    $('#results').html(html);
});

or

$('#results').load('test.html');

RightJS

Xhr.load('test.html', {
    onSuccess: function(request) {
        $('#results').html(request.text);
    }
}).send();

or

$('results').load('test.html');

Animations

jQuery

$('#menu').animate({ opacity: 1 }, 600);

RightJS

$('menu').morph({ opacity: 1 }, { duration: 600 });

Array traversal

jQuery

$.each(myArray, function(index, element) {
    console.log(element);
});

RightJS

myArray.each(function(element) {
    console.log(element);
});
Anurag
  • 140,337
  • 36
  • 221
  • 257
  • Anurag - Thanks for the answer and for doing the breakdown. I spent the better part of last night reading through the docs, and it does look very interesting. I like that it prototypes some of the native types like Array when needed to bring them into conformity (and then extends beyond spec too). Not sure why jQuery doesn't do this. Things like "call by name" functions looks slick as well. http://rightjs.org/tutorials/call-by-name The benchmarks provided look good, but I'll probably do my own one of these days. Overall, it looks pretty nice (on the surface anyway). Thanks for the v2 link too! – user113716 Aug 29 '10 at 21:23
  • @Patrick - thanks for pointing out call by name - I found a couple more cool features - added all to answer. thanks for the heads up on call by name :) – Anurag Aug 30 '10 at 02:16
  • Anurag - Very nice additions. The more I read about it, the more I like it. Haven't tried it yet, probably start on that tomorrow. Overall, I think it's got some extremely compelling features. One thing in your answer, unless I misunderstand, I don't think you'll be able to do `document.body.appendChild(calendar);`. I think the change is that you're no longer working directly with DOM elements, but with "wrapped" elements, like jQuery objects, but with greater capacity for subclassing, encapsulation, etc. (or something like that). – user113716 Aug 30 '10 at 02:32
  • ...Anyway, I think I'll leave this question open for a week or so. I don't think there are too many RightJS users out there right now, so I want to give them a chance to chime in. If I stumble across any mind-blowing features, I'll leave a note here. :o) – user113716 Aug 30 '10 at 02:33
  • Anurag - The creator of RightJS left an answer that may interest you. :o) – user113716 Aug 30 '10 at 17:04
  • @Patrick - thanks for the notification :) interesting points in @Nikolay's answer – Anurag Aug 30 '10 at 17:50
10

Hey folks, Nikolay, the author of RightJS is in here.

Thanks to Anurag, he described RightJS pretty well. And couple of notes though.

You actually already can mix the String#on and calls by name just as you described

"div.boo".on('click', 'toggleClass', 'marked');

Then in RightJS2 which is going to have the RC2 release right about tomorrow you'll be able to do things like

"div.boo".onClick('toggleClass');
"div.boo".observes('click'); // true
"div.boo".stopObserving('click');

And you also will be able to use any custom events with those, just like that

"div.boo".on('something', 'addClass', 'something-happened');

Custom events in RightJS2 bubble, have targets and everything you need.

Then about the dom wrappers and Calendar, yes elements and widgets will be in the same hierarchy of dom-wrappers and you will be able to toss them around just like that

$(document.body).insert(new Calednar(...));

You also will be able to manipulate with them on the dom level using direct access to the raw dom-object, like that

var calendar = new Calendar();
document.body.appendChild(calendar._);

BTW: Patrick. If you by any chance use Rails, you should also check out the right-rails plugin, there are quite a few really nice things on JavaScript and Rails integration.

Then if you ask about the genuine feeling working with RightJS, I would say it depends. RightJS was build for server-side folks who used to work with classes and objects, there are quite a few things for quick and agile development to solve the easy problems the easy way, but to get the most of it you need think in objects. If you also happened to have experience with Prototype or Ruby, most of the things should be pretty familiar, I tried to reuse as much method names as I could.

If all you know is jQuery, some things might look a bit strange from the beginning, but I saw several guys who happily migrated from jQuery. So I guess you'll be fine.

As for the code readability, imho it absolutely kicks ass. Readability was one of the primary concerns in RightJS development, you can read most of the code just like a plain English

"div.zing".on('click', 'toggleClass', 'marked');
$('my-element').update('with some text').highlight();
$$('li').each('onClick', 'toggleClass', 'marked');

And so one. Check this page, if you haven't yet http://rightjs.org/philosophy

This is about it. Ask if you have any more questions.

Nikolay
  • 1,392
  • 1
  • 9
  • 15
  • Thanks for taking the time to respond. It all looks very interesting. With regard to `calendar._` I assume that `._` will be the standard way of pulling DOM element(s) out of the wrapper? No, I don't use `Rails`. I'm "shopping around" right now. If I use a Ruby-based framework, it will likely be something more like `Ramaze` or `Sinatra`. I've also looked at building something around `NodeJS`. (I think that's where I heard about `RightJS`?) I posted the note about `Object.empty()` yesterday and you responded quickly. Is github the proper place for bug reports? – user113716 Aug 30 '10 at 16:33
  • @patrick yes `._` will be the uniformed way to access a raw object across all the dom-wrappers, including wrappers for windows, document, events, widgets, and so one. And yes I have a server-side build of RightJS which can be used with NodeJS. About bugs-tracking mainly I use lighthouse http://rightjs.lighthouseapp.com/projects/31989-core/overview . there is also a google group and an IRC channel, you can find them on the contacts page at rightjs.org – Nikolay Aug 30 '10 at 16:38
  • Thanks again. Yes, I'm most familiar with jQuery. I like its ease of use, but the additional flexibility that RightJS seems to provide may win me over. One thing I'll probably do is change the naming of the `empty()` methods by referencing them with my own `isEmpty()` methods. Not a big deal. Keep up the good work! :o) – user113716 Aug 30 '10 at 17:03
  • any plans of making strings usable wherever the `$` function would have been used? Such as `'myElement'.load('test.html')`? – Anurag Aug 30 '10 at 17:54
  • @Anurag - not now, I've already have too much with the 2.0.0 release. Maybe later. You could write a plugin, if you want those things, RightJS has all the nice tools for native extensions. If people will like it we might include in the core some day. – Nikolay Aug 31 '10 at 06:45
5

I'm a RightJS convert. I'm impressed with the philosophy, he seems to acknowledge that DOM elements are just data, where jQuery seems to base its whole identity on the DOM.

I use jQuery in my dayjob but find myself needing to use it in combination with underscore.js to do nice functional programming things. In RightJS, you get lots of nice FP goodies available as methods on native objects.

Here is a small comparison showing how the same method works on plain arrays and dom collections in right js:

Some html:

<ul id="test">
    <li data-id="one">one</li>
    <li data-id="two">two</li>
    <li data-id="three">three</li>
</ul>

An array:

var test = [{"data-id":"one"},{"data-id":"two"},{"data-id":"three"}];

Filtering the html, pretty similar:

//jQuery:
var filtered = $("#test li").filter(function(element) {
    return $(element).attr("data-id") == "one";
});

//RightJS
var filtered = $$('#test li').filter(function(element) {
    return element.get("data-id") == "one";
});

Filtering the array:

//you can't do this with jquery because it is DOM-specific.
//you need something like underscore:

//underscore:
var filtered = _.select(test, function(element) {
    return element["data-id"] == "one";
});

//RightJS << uses the same .filter() method as used on the html!
var filtered = test.filter(function(element) {
    return element["data-id"] == "one";
});

In RightJS, I would like to see some additional things that underscore has, like .range() and the option to write in either 'FP' or 'OOP' style, so I can use RightJS exclusively. Guess I better contribute :)

Having said that, as I understand, the big focus with jQuery these days is mobile compatibility. If that is a major concern, it still might be best to go with jQuery.

twfarland
  • 51
  • 2
  • Thanks for your input. :o) Curious about your example, though. jQuery has its own `filter()` method like RightJS, so why use underscore for that? – user113716 Sep 19 '10 at 22:17
  • Sorry it was a bad example. I should have extended it to show that you can use the same rightjs .filter() method on a normal array, but you can't apply the jquery .filter() method to a normal array. It only works on jquery collections. – twfarland Sep 28 '10 at 10:36
  • With the underscore example, you can also use `_(test).filter(function ...);`, and then it exactly mirrors the usage of jQuery.filter. You could even write a `$_` method that calls jQuery/underscore based on the arguments. My main beef with right.js is that it extends the prototype of the standard javascript types. If there's an optimizer/transpiler that will wrap the primitive types so that you still get the syntactic sugar, but don't incur the pitfalls of prototype extension messing with other people's code, that would make me accept right.js. – forivall Feb 19 '13 at 18:39
0

I think this is the best posible option to change in a natural way from prototypeJS. Is pretty pretty similar.

@Nikolay did you were a prototypeJS user?

ncubica
  • 8,169
  • 9
  • 54
  • 72