I am new to Backbonejs, trying to understand models here. I defined a model like this
var Employee=Backbone.Model.extend({
initialize: function(){
}
});
and set attributes like this. For one instance of model, added one more attributes but I didn’t any error message. I am just wondering about model definition here ! Could it be different attributes for different instances of model ?
var emp1=new Employee();
var emp2=new Employee();
var emp3=new Employee();
var emp4=new Employee();
emp1.set({eName:"Suresh",eType:"employee",eNationality:"Indian"});
emp2.set({eName:"Håkan",eType:"contractor"});
emp3.set({eName:"Pelle",eType:"employee"});
emp4.set({eName:"Per",eType:"employee"});
var empList=new Employees([emp1, emp2, emp3, emp4]);
console.log(empList.models);
Any help is appreciated !
Thanks.
Short answer: you can set any attributes you want on your Backbone.js model-instances, there’s no mechanism that forces instances in a collection to share the same attributes.
Practically speaking, the views/templates that the are used to display those instances will probably expect certain attributes to be present. If some attributes are optional, as in your example, you will have to take that into consideration when writing your views/templates, but it shouldn’t be a huge problem if the attribute-sets don’t diverge too much.