I have a Backbone.Model named Company
.
My Company
model has an Employees
Backbone.Collection containing Employee
models.
When I instantiate my Employee
models to populate the Employees
collection I wish for them to have a reference to the Company
they belong to. But when I pass Company
in it becomes one of the attributes of Employee
. This is an issue when I go to save the Employee
because the toJSON
method will contain a Company
object, when in the database all I store is the foreign key integer company_id
.
I wish there was a second parameter for Backbone.Model that accepted model properties that are not part of the core attributes. How can I get around this? I realize I could instantiate my Employee
models and afterwards attach the Company
, but I really want to do all the assignments in a traditional "constructor" rather than attach properties from outside.
e.g.:
Employee = Backbone.Model.extend({});
Employees = Backbone.Collection.extend({
model: Employee
});
Company = Backbone.Model.extend({
initialize: function() {
this.employees = new Employees({});
}
});
c1 = new Company({id: 1});
e = new Employee({name: 'Joe', company_id: 1, company: c1});
c1.employees.add(e);
e.get('company'); // => c1
e.save(); // BAD -- attempts to save the 'company' attribute, when in reality I only want to save name and company_id
//I could do
c2 = new Company({id: 2});
e2 = new Employee({name: 'Jane', company_id: 2});
e2.company = c2;
c2.employees.add(e);
e.company; // => c2
//I don't like this second method because the company property is set externally and I'd have to know it was being set everywhere in the code since the Employee model does not have any way to guarantee it exists
js Get model is used to get the value of an attribute on a model. Syntax: model. get(attribute)
model() You have to override the model property of the collection class to specify the model class that the collection contains. You can pass raw attributes objects to add, create and reset and the attributes will be converted into a model of the proper type.
The Backbone. js collection models specify the array or models which are created inside of a collection. Syntax: collection.
You could always read it manually from the options
object and store it however you like. The options are passed as the second argument to the initialize method:
var Employee = Backbone.Model.extend({
initialize: function(attributes, options) {
this.company = options.company;
}
});
var shesek = new Employee({name: 'Nadav'}, {company: Foobar});
Alternatively, you can use Backbone-relational which makes it much easier to handle models that contains references to other models and collection.
You might also be interested in making toJSON() recursive (a patch I submitted to their issue tracker)
You can override the save method on the model to convert the save record to only send name and company id.
Employee = Backbone.Model.extend({
save: function() {
var toSend = {name:this.get('name'),company_id: this.get('company').get('id')}
// method to send toSend to server.
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With