backbone--model--Study Notes

Source: Internet
Author: User

First, models-model

1. Create an empty note model

var Note = Backbone.Model.extend ({});

2. Instantiate the note model

1). Instantiate an empty model

var note1 = new Note ()

2). Instantiate the model and add properties

var note2 = new Note ({

Title: ' Backbone study Notes ',

Created_at:new Date ()

});

3. How the Model operates

1). Get Property values

Note2.attributes;

Note2.tojson (); Return to an Object

Json.stringify (NOTE2); Return JSON format data

2). Returns whether the object collection contains the specified key value, contains a return of true, otherwise returns false

Note2.has (' title ');

3). Gets the value of the specified property

Note2.get (' title '); Returns the value of the title property

4). Delete the value of the specified property

Note2.unset (' title ');

5). When you set the value of the specified property to have this property, change the value, without adding this property and the value

Note2.set (' title ', ' Backbone study notes ');

6). Clears all properties in the object

Note2.clear ();

4. Set default properties for the model

var Note = Backbone.Model.extend ({

Defaults: {

Title: ",

Created_at:new Date ()

}

});

5. Initialization of the model

Initialize: This method is called immediately when it is instantiated

var Note = Backbone.Model.extend ({

Defaults: {

Title: ",

Created_at:new Date ()

},

Initialize:function () {

Console.log (' Newly created a note: ' + this.get (' title '));

}

});

6. When a property is changed, the event that is triggered changes

var Note = Backbone.Model.extend ({

Defaults: {

Title: ",

Created_at:new Date ()

},

Initialze:function () {

Console.log (' Newly created a note: ' + this.get (' title '));


This.on (' Change ', function (model, options) {

Console.log (' The value of the attribute has changed ');

});


This.on (' Change:title ', function (model, options) {

Console.log (' title attribute has changed ');

});

}

});

7. Verify the value of the attribute validate

Attributes: Object Properties

Options: parameter of the option

var Note = Backbone.Model.extend ({

Defaults: {

Title: ",

Created_at:new Date ()

},


Initialize:function () {

Console.log (' Newly created a note: ' + this.get (' title '));


This.on (' Change ', function (model, options) {

Console.log (' The value of the attribute has changed ');

});


This.on (' Change:title ', function (model, options) {

Console.log (' title attribute has changed ');

});

},


Validate:function (attributes, Options) {

if (Attributes.title.length < 3) {

Return ' note has a title number greater than 3 ';

}

}


});

8. Event invalid after validation failed

Model: Object

Error: Wrong message

var Note = Backbone.Model.extend ({

Defaults: {

Title: ",

Created_at:new Date ()

},


Initialize:function () {

Console.log (' Newly created a note: ' + this.get (' title '));


This.on (' Change ', function (model, options) {

Console.log (' The value of the attribute has changed ');

});


This.on (' Change:title ', function (model, options) {

Console.log (' title attribute has changed ');

});


This.on (' invalid ', function (model, error) {

Console.log (Error);

});

},


Validate:function (attributes, Options) {

if (Attributes.title,length < 0) {

The title character of the return ' note should be greater than 3 ';

}

}

});


This article is from the "Network" blog, be sure to keep this source http://lipq09.blog.51cto.com/856751/1676749

backbone--model--Study Notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.