Note If collectionType attribute is specified, relatedModel is ignored.
Note In v0.5.2+, the map API is augmented to pass in a type parameter. The type parameter contains the target model (or collection) type which the passed-in id (or ids) will be eventually transformed to. Additionally, the map function is now invoked with the specific model instance as context. (versus a global function in v0.5.1)
v0.5.5+
Specify remoteKey to serialize the key to a different key name in toJSON() calls. Useful in ROR nested-attributes like scenarios.v0.5.5+
Default value is false. If isTransient is set to true, then the attribute will not be serialized in toJSON() calls.v0.6.0+
Use this attribute to set an array of attributes (singleton or subset) which will be serialized to the server end-point. Default behavior is to serialize all attributes.var Employee = Backbone.AssociatedModel.extend({ relations: [ { type: Backbone.One, //nature of the relationship key: 'manager', // attribute of Employee relatedModel: 'Employee' //AssociatedModel for attribute key } ], defaults: { age : 0, fname : "", lname : "", manager : null } });
var Location = Backbone.AssociatedModel.extend({ defaults: { add1 : "", add2 : null, zip : "", state : "" } }); var Project = Backbone.AssociatedModel.extend({ relations: [ { type: Backbone.Many,//nature of the relation key: 'locations', //attribute of Project relatedModel:Location //AssociatedModel for attribute key } ], defaults: { name : "", number : 0, locations : [] } });To specify 1:M associations, one can alternatively use the collectionType property. This can be useful to specify implementation of custom or built-in properties at the Collection level.
var Location = Backbone.AssociatedModel.extend({ defaults: { add1 : "", add2 : null, zip : "", state : "" } }); var Locations = Backbone.Collection.extend({ comparator: function(c){ return c.get('number'); }, model: Location }); var Project = Backbone.AssociatedModel.extend({ relations: [ { type: Backbone.Many,//nature of the relation key: 'locations', //attribute of Project collectionType: Locations, //Collection to be used. relatedModel: Location //Optional } ], defaults: { name : "", number : 0, locations : [] } });
dept1 = new Department({ name:"R&D", number:"23" }); var emp1 = new Employee({ fname:"Tom", lname:"Hanks", age:41, sex:"M" }); var emp2 = new Employee({ fname:"Michelle", lname:"Pfiefer", age:42, sex:"F" }); emp1.set({"works_for":dept1}); emp2.set({"works_for":dept1}); dept1.parents //[emp1, emp2] myco = new Company({ name:"Github Inc" }); myco.set({research:dept1}); dept1.parents //[emp1, emp2, myco]Prevent memory leaks
//Could cause mem leaks if you set emp2 = undefined //emp2 = undefined; //dept1.parents.length === 3 //Should have been 2 emp2.cleanup(); emp2 = undefined; //dept1.parents.length === 2
var MasterType = Backbone.Model.extend({ defaults:{ id:"-1", type:"", validations:"" } }); store = new Backbone.Collection([ {id:1, type:"phone", validations:"regex to validate phone #s here"}, {id:2, type:'email', validations:"regex to validate email formats here"} ], {model:MasterType}); var Field = Backbone.AssociatedModel.extend({ relations:[ { type:Backbone.One, key:'type', relatedModel:MasterType, map:function (type) { //a local master store holding the master data reference return store.findWhere({type:type}); } } ], defaults:{ type:undefined, data:undefined, name:"" } }); var phone = new Field({name:'Residence Phone', type:'phone' }); var email = new Field({name:'Primary Email', type:'email'}); phone.get('type').get('validations') //regex to validate phone #s email.get('type').get('validations'), //regex to validate emailNote : v0.5.2+
emp.get('works_for.controls[0].locations[0].zip') //94404 //or emp.get('works_for').get('controls').at(0).get('locations').at(0).get('zip');
emp.set('works_for.locations[0].zip', 94403); //or emp.get('works_for').get('locations').at(0).set('zip',94403);