Skip to content

Commit 9ed7be4

Browse files
committed
References #2 - Releasing Backbone.validateAll v0.2.0
1 parent 95c1fee commit 9ed7be4

File tree

4 files changed

+29
-21
lines changed

4 files changed

+29
-21
lines changed

README.markdown

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Anyone who wants an **option** to validate only Model properties that are curren
5959

6060
#Set data on your model using the validateAll option
6161
// The validateAll option makes sure that only the Model attributes that you are setting get passed to the validate method
62-
user.set({ "firstname": "Greg" }, {validateAll: false});
62+
user.set({ "firstname": "Greg" }, {validate: true, validateAll: false});
6363

6464
##Unit Tests
6565
All unit tests are written using the Jasmine Framework
@@ -78,6 +78,11 @@ If you find that you need a feature that Backbone.validateAll does not currently
7878

7979
##Change Log
8080

81+
`.0.2.0` - January 23, 2013
82+
83+
- Upgraded to support **Backbone v0.9.10**
84+
- Fixed demo bug [#2](https://github.com/gfranko/Backbone.validateAll/issues/2)
85+
8186
`0.1.0` - August 29, 2012
8287

8388
- Initial Backbone.validateAll release. Added annotated source code, unit tests, and documentation

demos/index.html

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
<meta charset=utf-8>
55
<title>Form Validation - Model#validate</title>
66
<script src='http://code.jquery.com/jquery.js'></script>
7-
<script src='http://cdnjs.cloudflare.com/ajax/libs/lodash.js/0.6.0/lodash.min.js'></script>
8-
<script src='http://backbonejs.org/backbone.js'></script>
9-
<sript src='../src/javascript/Backbone.validateAll.js'></script>
7+
<script src='http://cdnjs.cloudflare.com/ajax/libs/lodash.js/0.10.0/lodash.min.js'></script>
8+
<script src='http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.10/backbone-min.js'></script>
9+
<script src='../src/javascripts/Backbone.validateAll.js'></script>
1010
<script>
1111
jQuery(function($) {
1212

@@ -116,7 +116,11 @@
116116

117117
}
118118

119-
if (!_.isEmpty(errors)) return errors;
119+
if (!_.isEmpty(errors)) {
120+
121+
return errors;
122+
123+
}
120124
}
121125
});
122126

@@ -127,7 +131,14 @@
127131
this.$msg = $('[data-msg=' + this.name + ']');
128132
},
129133
validate: function() {
130-
this.model.set(this.name, this.$el.val(), {validateAll: true});
134+
var self = this,
135+
obj = {};
136+
obj[this.name] = this.$el.val();
137+
this.model.set(obj, {validate: true, validateAll: false}, { error: function(model, obj) {
138+
console.log(obj);
139+
}}
140+
);
141+
131142
this.$msg.text(this.model.errors[this.name] || '');
132143
}
133144
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "Backbone.validateAll.js",
33
"title": "Backbone.js plugin that provides an option to only validate Model properties that are currently being set or saved",
44
"description": "This plugin originated out of frustration with using the Backbone.js Model `validate` method when validating HTML forms. Since Backbone.js v0.9.1 and greater, Backbone Model validation was not made to elegantly handle form validation, since the default `validate` method will validate all Model attributes, regardless of what particular attribute is being **set** or **saved**. For certain use cases, it is necessary to only validate a certain Model property (or form field) without worrying about the validation of any other Model property (or form field).",
5-
"version": "0.1.0",
5+
"version": "0.2.0",
66
"homepage": "https://github.com/gfranko/Backbone.validateAll",
77
"author": {
88
"name": "Greg Franko",

src/javascripts/Backbone.validateAll.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,22 @@
1-
/* Backbone.validateAll.js - v0.1.0 - 2012-08-29
1+
/* Backbone.validateAll.js - v0.2.0 - 2013-01-23
22
* http://www.gregfranko.com/Backbone.validateAll.js/
33
* Copyright (c) 2012 Greg Franko; Licensed MIT */
44

55
// Locally passes in the `window` object, the `document` object, and an `undefined` variable. The `window` and `document` objects are passed in locally, to improve performance, since javascript first searches for a variable match within the local variables set before searching the global variables set. All of the global variables are also passed in locally to be minifier friendly. `undefined` can be passed in locally, because it is not a reserved word in JavaScript.
6-
(function (window, document, undefined) {
6+
;(function (window, document, undefined) {
77
// Checks to make sure Backbone, Backbone.Model and the private validate method are on the page
88
if(window.Backbone && window.Backbone.Model && window.Backbone.Model.prototype._validate) {
99
// Run validation against the next complete set of model attributes,
1010
// returning `true` if all is well. If a specific `error` callback has
1111
// been passed, call that instead of firing the general `"error"` event.
1212
window.Backbone.Model.prototype._validate = function(attrs, options) {
13-
if (options.silent || !this.validate) {
14-
return true;
15-
}
13+
if (!options.validate || !this.validate) return true;
1614
if (options.validateAll !== false) {
1715
attrs = _.extend({}, this.attributes, attrs);
1816
}
19-
var error = this.validate(attrs, options);
20-
if (!error) {
21-
return true;
22-
}
23-
if (options && options.error) {
24-
options.error(this, error, options);
25-
} else {
26-
this.trigger('error', this, error, options);
27-
}
17+
var error = this.validationError = this.validate(attrs, options) || null;
18+
if (!error) return true;
19+
this.trigger('invalid', this, error, options || {});
2820
return false;
2921
};
3022
}

0 commit comments

Comments
 (0)