From 7a8ac35ea00a963daccea6526b9bdf729d73ab15 Mon Sep 17 00:00:00 2001 From: ledbutter Date: Thu, 4 Dec 2014 14:46:05 -0600 Subject: [PATCH] Update event binding and force validation on set --- _posts/2011-01-29-what-is-a-model.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/_posts/2011-01-29-what-is-a-model.md b/_posts/2011-01-29-what-is-a-model.md index d6eac751..42e80a6f 100644 --- a/_posts/2011-01-29-what-is-a-model.md +++ b/_posts/2011-01-29-what-is-a-model.md @@ -294,19 +294,21 @@ _Validate data before you set or save it_ }, initialize: function(){ alert("Welcome to this world"); - this.bind("error", function(model, error){ + // The invalid event is used to handle validation errors on the client-side + this.bind("invalid", function(model, error){ // We have received an error, log it, alert it or forget it :) alert( error ); }); } }); + // By default, Backbone will only validate models on save, not set. We can override that by passing in validate: true var person = new Person; - person.set({ name: "Mary Poppins", age: -1 }); + person.set({ name: "Mary Poppins", age: -1 }, {validate: true}); // Will trigger an alert outputting the error var person = new Person; - person.set({ name: "Dr Manhatten", age: -1 }); + person.set({ name: "Dr Manhatten", age: -1 }, {validate: true}); // God have mercy on our souls {% endhighlight %}