error events are now always passed the model as the first argument. You may now also pass an error callback to set() and save(), if the callback is passed, it will be called instead of the 'error' event getting fired.

This commit is contained in:
Jeremy Ashkenas
2010-10-19 10:13:50 -04:00
parent b854b28d18
commit a09bcbca9d
2 changed files with 41 additions and 9 deletions

View File

@@ -138,4 +138,28 @@ $(document).ready(function() {
equals(lastError, "Can't change admin status.");
});
test("Model: validate with error callback", function() {
var lastError, boundError;
var model = new Backbone.Model();
model.validate = function(attrs) {
if (attrs.admin) return "Can't change admin status.";
};
var callback = function(model, error) {
lastError = error;
};
model.bind('error', function(model, error) {
boundError = true;
});
var result = model.set({a: 100}, {error: callback});
equals(result, model);
equals(model.get('a'), 100);
equals(lastError, undefined);
equals(boundError, undefined);
result = model.set({a: 200, admin: true}, {error: callback});
equals(result, false);
equals(model.get('a'), 100);
equals(lastError, "Can't change admin status.");
equals(boundError, undefined);
});
});