Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone-associations vs Backbone-relational

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

  • Backbone-relational
  • Backbone-associations

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?
like image 734
brg Avatar asked Aug 12 '14 05:08

brg


2 Answers

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.

like image 123
Ernests Karlsons Avatar answered Sep 23 '22 01:09

Ernests Karlsons


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

like image 44
gfd Avatar answered Sep 25 '22 01:09

gfd