pathable / supermodel

Supermodel - Minimal Model Tracking for Backbonejs
http://pathable.github.io/supermodel
MIT License
229 stars 36 forks source link

New feature: Local relationships #66

Closed nachocodoner closed 8 years ago

nachocodoner commented 9 years ago

Hi everyone!

Supermodel is an amazing Backbone plugin for model tracking. It creates a lightweight solution. However, there are some features I miss by this kind of library. Because of that, I have been working on them and I will bring them up to you little by little.

Basically, I have had the necessity to enable Supermodel to support local realtionships, serialization options and inheritance features. I bring you local relationships at this time.

Local relationships

Supermodel provides a way to make relationships between models. Yet, it is not able to set them up unless you have a remote identification (idAtributte). It steps away from the idea of building offline web applications. For this reason, I have enabled "Supermodel" the possibility to make relationships between local models (identified by cid) and when a remote identification is available, it will change references automatically. It will work for all kind of relationships supported by "Supermodel" (one-to-one, many-to-one and many-to-many).

Considerations

// User has one Settings, Settings has one User
var user = User.create();
var settings = Settings.create();

// There is not an id available neither user nor settings model
user.settings(settings);
// or user.set({settings_id: settings.cid}) also works

// association is set
user.settings() == settings; // true :)
settings.user() == user; // true :)

// Then a remote id is available
user.set({id: 1});

// hence it updates references automatically
settings.get("user_id") == user.id; // true :D

Many-to-one example

// User has many Membership, Membership has one User
var user = User.create();
var membership = Membership.create();

// There is not an id available neither user nor settings model
user.memberships().add(membership);

membership.user() == user; // true :)

// Then a remote id is available
user.set({id: 1});

// hence it updates references automatically
membership.get("user_id") == user.id; // true :D

I hope you enjoy it! I will bring the rest during the next week.

PD: This branch includes some corrections and test fixing I made for newer versions of Backbone.js. I made a pull request formerly. (https://github.com/pathable/supermodel/pull/65)

flippyhead commented 9 years ago

This looks like really great stuff. There's merge conflicts looks like with another pull of yours actually. Can you take a look while we finish our review? Thanks very much!

nachocodoner commented 9 years ago

Thanks, I have checked it and yes, a merge option was not needed when "many-to-*" associations are being built. It is now just done as formerly. The conflict is solved.

flippyhead commented 9 years ago

Oops, looks like not again ;)

nachocodoner commented 9 years ago

Conflicts are resolved. :P