Contact.Models.Dialog = Backbone.Model.extend({ defaults:{ dialogId: "", title: "", html: "", btnvalue: "" }
Defining a model requires inheriting only the backbone model, so that our models have methods and attributes such as get,set,destory, we can rewrite them, or we can define our own methods to manipulate model.
Defaults: Defines the default properties of the model and the default values for those properties, or you can not define
Bacbkone is not strictly MVC, so even if we define a dialog model, we can put objects in this model at a later time, such as:
var tmpDialog = new Contact.Models.Dialog({ aaa : "ssss", bb: "xxx"
At this point, the attributes in Tmpdialog are like this, we can use model.attributes
to view all the properties of model:
At this point we can see that the attributes are:
{ aaa : "ssss", bb: "xxx", dialogId: "", title: "", html: "", btnvalue: ""
In addition to these attributes, backbone automatically assigns a unique ID to each model, and can get its value by Model.id.
When we new a backbone object, we automatically call the Constructor/initialize method, and we can override this method to achieve some effect.
We can also inherit our model (or view or collection), such as we now need a special window, in addition to the above several attributes, it has special properties and methods, such as:
Contact.Models.SpecialDialog = Contact.Models.Dialog.extend({ defaults: { sepcolum: "xxx" }, sepMethod: function(){ //you codes }
At this time our specialdialog has all the attributes and methods of dialog, and has its own special properties and methods.
Backbone provides us with a lot of scaffolding, more properties and methods on model please check out the official documentation for backbone.
The code for the contact model is attached below:
Contact.Models.Member = Backbone.Model.extend({ defaults:{ name: "", mobile: "", email: "", selected: false }, //切换选中 toggle: function(){ this.set({ selected: !this.get("selected") }); }