Skip to content

Commit

Permalink
Added model and an array of invalid attribute names as arguments to t…
Browse files Browse the repository at this point in the history
…he events
  • Loading branch information
thedersen committed Jan 8, 2012
1 parent 3c2e97c commit 1d17dd6
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 28 deletions.
25 changes: 21 additions & 4 deletions README.md
Expand Up @@ -108,11 +108,23 @@ If you have set the global selector to class, you can of course set the selector

## Events

The model triggers two events after validation is performed

* 'validated' with `true` as argument when model is valid or 'validated' with `false` as argument when model is invalid
* 'validated:valid' when model is valid or 'validated:invalid' when model is invalid
The model triggers two events, 'validated' and 'validated:valid' or 'validated:invalid', after validation is performed.

mode.bind('validated', function(isValid, model, attrs) {
// isValid is true or false
// model is the model
// attrs is an array with the name(s) of the attribute(s) with error (or undefined when model is valid)
});

mode.bind('validated:valid', function(model) {
// model is the model
});

mode.bind('validated:invalid', function(model, attrs) {
// model is the model
// attrs is an array with the name(s) of the attribute(s) with error
});

## The built-in validators

#### method validator
Expand Down Expand Up @@ -354,6 +366,11 @@ The message can contain placeholders for arguments that will be replaced:

# Release notes

### v0.3.1

* Fixed issue with validated events being triggered before model was updated
* Added model and an array of invalid attribute names as arguments to the events

### v0.3.0

* Triggers events when validation is performed (thanks to [GarethElms](https://github.com/GarethElms)):
Expand Down
7 changes: 4 additions & 3 deletions backbone.validation.js
Expand Up @@ -84,13 +84,14 @@ Backbone.Validation = (function(Backbone, _, undefined) {
return model.validate.call(model, _.extend(getValidatedAttrs(model), model.toJSON()));
}

var result = [];
var result = [], invalidAttrs = [];
isValid = true;

for (var changedAttr in attrs) {
var error = validateAttr(model, changedAttr, attrs[changedAttr]);
if (error) {
result.push(error);
invalidAttrs.push(changedAttr);
isValid = false;
invalidFn(view, changedAttr, error, selector);
} else {
Expand All @@ -108,8 +109,8 @@ Backbone.Validation = (function(Backbone, _, undefined) {
}

_.defer(function() {
model.trigger('validated', isValid);
model.trigger('validated:' + (isValid ? 'valid' : 'invalid'));
model.trigger('validated', isValid, model, invalidAttrs);
model.trigger('validated:' + (isValid ? 'valid' : 'invalid'), model, invalidAttrs);
});

if (result.length === 1) {
Expand Down
76 changes: 55 additions & 21 deletions tests/events.js
Expand Up @@ -39,23 +39,24 @@ buster.testCase("Backbone.Validation events", {
},

"when model is valid": {
"validated event is triggered with true": function(done) {
this.model.bind('validated', function(valid){
"validated event is triggered with true and model": function(done) {
this.model.bind('validated', function(valid, model){
assert(valid);
assert.same(this.model, model);
done();
});
}, this);

this.model.set({
age: 1,
name: 'name'
});
},

"validated:valid event is triggered": function(done) {
this.model.bind('validated:valid', function(){
assert(true);
"validated:valid event is triggered with model": function(done) {
this.model.bind('validated:valid', function(model){
assert.same(this.model, model);
done();
});
}, this);

this.model.set({
age: 1,
Expand All @@ -64,44 +65,77 @@ buster.testCase("Backbone.Validation events", {
}
},

"when model is invalid": {
"validated event is triggered with false": function(done) {
this.model.bind('validated', function(valid){
"when one invalid value is set": {
"validated event is triggered with false, model, and name of attribute with error": function(done) {
this.model.bind('validated', function(valid, model, attr){
refute(valid);
assert.same(this.model, model);
assert.equals(['age'], attr);
done();
});
}, this);

this.model.set({age:0});
},

"validated:invalid event is triggered": function(done) {
this.model.bind('validated:invalid', function(){
assert(true);
"validated:invalid event is triggered with model and name of attribute with error": function(done) {
this.model.bind('validated:invalid', function(model, attr){
assert.same(this.model, model);
assert.equals(['age'], attr);
done();
});
}, this);

this.model.set({age:0});
},

"error event is triggered with error as a string when one invalid value was set": function(done) {
"error event is triggered with model and error as a string": function(done) {
this.model.bind('error', function(model, error) {
assert.same(this.model, model);
assert.equals('age', error);
done();
});
}, this);

this.model.set({age:0});
}
},

"when two invalid values is set": {
"validated event is triggered with false, model, and an array with the names of the attributes with error": function(done) {
this.model.bind('validated', function(valid, model, attrs){
refute(valid);
assert.same(this.model, model);
assert.equals(['age', 'name'], attrs);
done();
}, this);

this.model.set({
age: 0,
name: ''
});
},

"error event is triggered with error as an array when two invalid values was set": function(done) {

"validated:invalid event is triggered with model and an array with the names of the attributes with error": function(done) {
this.model.bind('validated:invalid', function(model, attrs){
assert.same(this.model, model);
assert.equals(['age', 'name'], attrs);
done();
}, this);

this.model.set({
age: 0,
name: ''
}, this);
},

"error event is triggered with model and error as an array": function(done) {
this.model.bind('error', function(model, error) {
assert.equals(['age', 'name'], error);
done();
});
}, this);

this.model.set({
age: 0,
name: ''
});
}
}
}
});

0 comments on commit 1d17dd6

Please sign in to comment.