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]"
event: An existing Backbone event.
add, remove, update, reset, sort, change, etc
association: A Supermodel association name whether One, ManyToOne or ManyToMany.
etc: A parameter such as an attribute in case of change event or a route name for a route event.
Replace event
"replace:[one-association]" (model, other)
When a model is linked to another model in a "One" association.
model The model on linking
other The model being associated
Examples
var user = User.create();
var group = Group.create();
var settings = Settings.create();
var membership = Membership.create();
'One' association example
User has One Settings
/* 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
User has Many Group
/* 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
User has Many Group through Memberships
Group has Many User through Memberships
/* 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);
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]"
Replace event
"replace:[one-association]" (model, other)
When a model is linked to another model in a "One" association.
Examples
'One' association example
'ManyToOne' association example
'ManyToMany' association example