1.

What Are The Tips/tricks/practice, With Respect To Model, You Follow?

Answer»

Below are some practices you should follow while working with Backbone models:

Get all the Current ATTRIBUTES

obj.attributes gives a direct reference to the attributes and you should be careful when playing with it. Best practice would SUGGEST that you use .set() to edit attributes of a model to take advantage of backbone listeners.

var person = new Person({ NAME: "Thomas", age: 67});

var attributes = person.toJSON(); // { name: "Thomas", age: 67}

/* This simply returns a copy of the current attributes. */

var attributes = person.attributes;

Validate Data Before You Set or Save It

Always implement validate() method so that Backbone validates the input before setting any attribute value. It ensures that invalid data is not stored on your server.

Person = Backbone.Model.extend({
// If you return a string from the validate function,Backbone will throw an error
validate: function( attributes ){
if( attributes.age < 0 && attributes.name != "Dr Manhatten" ){
return "You can't be negative YEARS old";
}
},
initialize: function(){
//Bind callback for error event
this.bind("error", function(model, error){
// We have received an error, log it, alert it or forget it <img alt=":)" class="wp-smiley" SRC="http://synvistech.com/blogs/wp-includes/images/smilies/simple-smile.png" style="height: 1em; max-height: 1em;" />
alert( error );
});
}
});

var person = new Person;
person.set({ name: "Mary Poppins", age: -1 }); 
// Will trigger an alert outputting the error

var person = new Person;
person.set({ name: "Dr Manhatten", age: -1 });

Below are some practices you should follow while working with Backbone models:

Get all the Current Attributes

obj.attributes gives a direct reference to the attributes and you should be careful when playing with it. Best practice would suggest that you use .set() to edit attributes of a model to take advantage of backbone listeners.

var person = new Person({ name: "Thomas", age: 67});

var attributes = person.toJSON(); // { name: "Thomas", age: 67}

/* This simply returns a copy of the current attributes. */

var attributes = person.attributes;

Validate Data Before You Set or Save It

Always implement validate() method so that Backbone validates the input before setting any attribute value. It ensures that invalid data is not stored on your server.

Person = Backbone.Model.extend({
// If you return a string from the validate function,Backbone will throw an error
validate: function( attributes ){
if( attributes.age < 0 && attributes.name != "Dr Manhatten" ){
return "You can't be negative years old";
}
},
initialize: function(){
//Bind callback for error event
this.bind("error", function(model, error){
// We have received an error, log it, alert it or forget it <img alt=":)" class="wp-smiley" src="http://synvistech.com/blogs/wp-includes/images/smilies/simple-smile.png" style="height: 1em; max-height: 1em;" />
alert( error );
});
}
});

var person = new Person;
person.set({ name: "Mary Poppins", age: -1 }); 
// Will trigger an alert outputting the error

var person = new Person;
person.set({ name: "Dr Manhatten", age: -1 });



Discussion

No Comment Found