Backbone-associations vs Backbone-relational
Asked Answered
Z

3

6

I have been looking for information about how can we build relationship in Backbone and came across following two nice plugins:

Both seems to have exist more than two years and seems to be stable. However, Backbone-relational outshines against Backbone-associations in following terms:

  • Providing almost all relations like one-to-one, one-to-many, many-to-one as we have in Database
  • Nice documentation (similar to Backbone.js) on first glance

Since, I haven't had time to go through both the plugins extensively, I would like to know from the experienced person following things:

  • Does both support AMD (like Requirejs)?
  • How easy to use the plugin with back-end-server like Ruby on Rails?
  • How easy to implement polymorphic relationship?
Zante answered 12/8, 2014 at 5:49 Comment(3)
So, have you tried both (given that nobody replied :/ ) ? On a new project I'm working on, we use bb-relational (quite easily it seems), but I'm been looking at these two for another project: I'd appreciate your feedback...Stow
I'm part way through a migration from Relational to Associations, because Relational turned out to be a pain, mostly because of its "store" getting in the way. When I know more, I'll reply with an answer. Just so you know this question does still show up :)Shawm
I started using backbone-relational in a large BB + marionette app and found memory leaks and performance issues. Looked at backbone-associations and SuperModel, both looked ok... Ended up just overriding get|set ourselves, didnt need all of the 'features' these libraries haveUnderbody
M
2

Biggest difference is that Backbone-relational fobids creating multiple instances of same model with identical ids. Consider:

let Person = Backbone.RelationalModel.extend({
    relations: [
        type: Backbone.HasMany,
        key: 'likes_movies',
        relatedModel: 'Movie'
    ]
});

let peter = new Person({
    likes_movies: [{id: 1, title: 'Fargo'}, {id: 2, title: 'Adams Family'}]
);

let john = new Person({
    likes_movies: [{id: 1, title: 'Fargo'}, {id: 2, title: 'Adams Family'}]
);

// Change title of one of Peter's movies
peter.get('likes_movies').get(1).set('title', 'Fargo 2 (Sequel)');

// John's corresponding movie will have changed its name
console.log(john.get('likes_movies').get(1)); // Fargo 2 (Sequel)

If rewritten for Backbone-associations, the movie title for John wouldn't have changed. This can be considered a feature or a disadvantage, depending on how you look at it.

Besides this, both libraries are very similar, except that development of Backbone-associations seems to have stopped almost a year ago.

Modular answered 15/10, 2015 at 18:41 Comment(0)
S
1

Actually, based on both GitHub pulse (activity indicator), Backbone-relational community seems much more active.

Stow answered 13/2, 2015 at 19:20 Comment(0)
B
0

I've studied both of these libraries when I was looking for something like EmberData or Restangular for Backbone.

Both of these libraries try to make up for the main Backbone weakness: handle properly Rest API Restful Resources.
In fact, Backbone promotes new models creation each time it need to be rendered (instead of reusing the instance used for another rendering somewhere else on the application).
Some inconsistencies then occurs because some model updates are not propagated everywhere on the web-application.
A cache of backbone model instances is then required.. Backbone Relational provides such a cache but Backbone Association doesn't.

Furthermore, both have reimplemented the code methods of Backbone (set, get, reset, trigger) so they are strongly coupled with Backbone.
That could complexify the Backbone library migrations especially if you use another MVC framework on top of Backbone (Marionnette, Thorax, Chaplin,...)

Backbone Association is lighter than Backbone Relational in terms of lines of code (800 vs 2000).

  • Backbone Association implementation is easier to debug because it directly manages relationships into the overloaded methods (set, get,...)
  • On the contrary, Backbone Relational relies on queues to synchronize relationship content with its internal store. That makes the debugging tricky...

Another lightweight (but less used) alternative is "Backbone SuperModel": http://pathable.github.io/supermodel/

  • This library is compound of 800 lines of code easier to understand than Backbone Relational (I was able to fix a little bug on it myself.)
  • It offers a backbone instance cache based on Backbone Collection

On my side,

  • I succeed to integrate the last one with RequireJs
  • I manage some polymorphic associations with it
  • A protocol between my web-application and my Java backend emerged
  • I succeed to upgrade Backbone and Marionette every time I need
Botheration answered 5/7, 2016 at 9:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.