10. Learn backbone.js tutorial from scratch for beginners(Part 13) Model Validation in backbone.js

C:\Users\Administrator\Desktop\gulp\app.js


jQuery(document).ready(function($) {
var FirstModel = Backbone.Model.extend({
initialize: function() {
this.validate();
},
validate: function(attributes) {
if(attributes && attributes.age < 1) {
return "Age must be negative.";
}
}
});
fmodel = new FirstModel({
});
fmodel.set({
name: "Tuong",
team: "Solution",
age: 0
});
var ModelView = Backbone.View.extend({
model: fmodel,
initialize: function() {
this.render();
},
render: function() {
console.log(this.model.toJSON());
}
});
vmodel = new ModelView();
});

C:\Users\Administrator\Desktop\gulp\app.js
jQuery(document).ready(function($) {
var FirstModel = Backbone.Model.extend({
initialize: function() {
this.validate();
},
validate: function(attributes) {
if(attributes && attributes.age < 1) {
return "Age must be negative.";
}
}
});
fmodel = new FirstModel({
});
fmodel.set({
name: "Tuong",
team: "Solution",
age: 0
});
var ModelView = Backbone.View.extend({
model: fmodel,
initialize: function() {
this.render();
},
render: function() {
if(this.model.isValid()) {
console.log("Valid values are false");
}else {
console.log(this.model.validationError);
}
console.log(this.model.toJSON());
}
});
vmodel = new ModelView();
});
Previous9. Learn backbone.js tutorial from scratch for beginners(Part 12) Model Events in backbone.jsNext11. Learn backbone.js tutorial from scratch for beginners(Part 14) Model Change Events in backbone.j
Last updated
Was this helpful?