Models in backbonejs (II)

Source: Internet
Author: User

Models in backbonejs (II)
1. Model identifier each model has an ID attribute used as a unique identifier to distinguish between different models. Through the id attribute, we can directly access the attributes used for identifier storage in the model object. The default attribute is id, but you can also change the name by setting idAttribute. For example, var _ model = Backbone. model. extend ({idAttribute: '_ id'}); testObj = new _ model ({...}) set id: testObj. _ id = Math. random (). toString (36), substr (2); get id: var id = testObj. _ id 2: model attribute verification (Backbone. model comes with validate) to control the specification of each value, we often need to verify the Model attributes. 1: Define the validate () method in the model for Property Verification var _ Model = Backbone. model. extend ({validate: function (attrs) {if (attrs. name! = 'Test') {return "name error info" ;}}); The attrs parameter contains the changed property value. If it is invalid, an error message is returned. 2: verification trigger time property verification will be triggered if {validate: true} is passed as the last parameter when the set () method is called when the save () method is called. For example, testObj. set ('name', 'test1', {validate: true}); // triggered verification when the name value is set. During model verification, you can still use get () and this. when attributes asks to change the previous attribute value, validate will be called before save (). The parameter is the updated attribute value sent from the save () method. If the verification is incorrect, save will not be executed, trigger invalid event 3: The error handler function for verification. Failed verification will trigger invalid event a: bind invalid event testObj through the module instance object. on ('invalid', function (model, error) {console. log (error)} B: the invalid function used for verification can be passed as a parameter to set (), save (), and so on .. TestObj. set ({name: 'test01'}, {invalid: function (model, error) {console. log (error)} 4: manually trigger verification ------ the isValid method manually checks whether a model is valid for testObj. isValid (), note: The isValid method does not trigger invalid error handler Function 3: by default, the advanced verification method is a model property verification method provided by Backbone. validate () needs to create a verification function by yourself, it takes a lot of time. Another Backbone extension Backbone. Validation provides many verification functions. You only need to download the plug-in and add references to it. Backbone. the Validation plug-in overrides Backbone. model's validate () method, so you can use the previous method to call the validate () and isValid () method operation steps: 1: during initialization, use Backbone. balidation. mixin to extend Backbone. object ()_. extend (Backbone. model. prototype, Backbone. validation. mixin) 2: define verification rules in the validation attribute, such as var _ model = Backbone. model. extend ({validation: {name: {required: true}, email: {pattern: 'email '}}); 3: use var testObj = new _ model (); testObj. set ({email: '*** @ qq. c Om '}, {validate: true}) // verification testObj is triggered only when email is set. isValid (); // false checks whether the module triggers verification testObj. get ('email '); // The email attribute of the undefine testObj instance has not yet been initialized. 4: validation's built-in validators required: verify whether the property is a required acceptance: verify whether the property value is accepted. For example, whether the attribute value of type boolean is true or false validation: {terms: {acceptance: true},} min: the attribute value must be a number and be greater than or equal to the specified minimum value max: the property value must be a number and be less than or equal to the specified maximum value range: the property value must be a number and be equal to or between two values range: [2, 18] length: Verify the length of the string minLength: verify that the string length is greater than or equal to the specified value maxLength: Verify that the string length is less than or equal to the specified value rangeLength: Verify that the string length is equal to or between the specified value oneOf: verify that the value must be one of the specified oneOf: ["person", "organization"] failed to: Verify that the property value must be equal to another specified property value, such as passwordRepeat: {failed: 'Password'} pattern: Verify that the attribute value is in the specified mode. Or a self-defined regular expression. Built-in mode: "number, digits, email, url" such as: phoneNumber: {pattern:/^ *********** $/} custom Regular Expression 4: use Backbone. validate the html form 1: bind the view to Backbone in the initialize () method of the view. var _ view = Backbone. view. extend ({initialize: function () {Backbone. validation. bind (this) ;}}) when a user updates the attribute value of a form, if the information is illegal, Backbone. validation adds an invalid style to the form element and sets an error message in the data-error attribute. For example: <input type = 'text' name = 'name' class = 'invalid' data-error = 'name is required'/>

Related Article

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.