pathable / supermodel

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

New features: Clone and association events #71

Closed nachocodoner closed 9 years ago

nachocodoner commented 9 years ago

Hi again! This time I bring to you two new features: cloning supermodels and association events.

I hope you enjoy it!

Clone

When you clone a Supermodel, via clone method, is not prepared to work as a new Supermodel instance. This patch enables you to use clone method and it will copy all attributes but associations. This is the default behaviour.

Supermodel.Model

cloneAttributes()

Object<attribute,value>. Return an object which represents the attributes to clone. By default, no associations are copied, only attributes. In case you wish to change the logic of which attributes or associations you want to be cloned by default, this method will be overrided.

Association events

Here's presented a feature to enable you to control events from associations for Supermodel instances.

supermodel.on("event", callback)

Catalog of events

All existing Backbone's events are supported for being listened by a particular model.

Existing event

"[event]:[association]:[etc]"

When a model is linked to another model in a "One" association.

var user = User.create();
var group = Group.create();
var settings = Settings.create();
var membership = Membership.create();
'One' association example
/* One listeners */
// Listens an event waiting a "settings" to be associated to the user.
user.on("replace:settings", function (model, other) {});

// Listens a change event on the "settings" model associated to the user, 
// concretly it listens to the "subscribed" attribute.
user.on("change:settings:subscribed", function (model, value, options) {});

/* Performing triggers */
// Triggers replace event
user.settings(settings);

// Triggers a change on "settings"
user.settings().set('subscribed', true);
'ManyToOne' association example
/* Many to One listeners */
// Listens an add event waiting a "group" being added to the user.
user.on("add:groups", function(model, collection, options) {});

// Listens a change event wainting a group associated to be changed.
user.on("change:groups", function(model, options) {});

/* Performing triggers */
// Triggers an addition of a group to a user.
user.groups().add(group);

// Trigger a change on a group added to the user
group.set("name", 'Supermodel Team');
'ManyToMany' association example
/* Many to Many listeners */
// Listens an user addition to a group.
group.on("add:users", function (model, collection, options) {});

// Listens a membership addition to a group.
group.on("add:memberships", function (model, collection, options) {});

// Listens a group addition to a user.
user.on("add:groups", function (model, collection, options) {});

// Listens a membership addition to a user.
user.on("add:memberships", function (model, collection, options) {});

/* Performing triggers */
// Triggers an user addition to a group explicitly, 
// a membership is triggered implicity.
group.users().add(user);

// Triggers a group addition to a user explicitly, 
// a membership is triggered implicitly.
user.groups().add(group);
flippyhead commented 9 years ago

Beautiful!