js_stack 0.0.4 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +16 -0
- data/README.md +16 -8
- data/js_stack.gemspec +3 -6
- data/lib/js_stack/version.rb +1 -1
- data/lib/js_stack.rb +0 -2
- data/vendor/assets/javascripts/js_stack/backbone/backbone-1.0.0.js +1571 -0
- data/vendor/assets/javascripts/js_stack/backbone/backbone-1.1.0.js +1581 -0
- data/vendor/assets/javascripts/js_stack/backbone.associations/backbone.associations-0.5.4.js +574 -0
- data/vendor/assets/javascripts/js_stack/backbone.associations.js +1 -1
- data/vendor/assets/javascripts/js_stack/backbone.js +1 -1
- data/vendor/assets/javascripts/js_stack/backbone.pageable/backbone.pageable-1.4.1.js +1345 -0
- data/vendor/assets/javascripts/js_stack/backbone.pageable.js +1 -1
- data/vendor/assets/javascripts/js_stack/backbone.validation/backbone.validation-0.9.0.js +634 -0
- data/vendor/assets/javascripts/js_stack/backbone.validation.js +1 -1
- data/vendor/assets/javascripts/js_stack/backbone.virtualcollection/backbone.virtualcollection-0.4.8.js +340 -0
- data/vendor/assets/javascripts/js_stack/backbone.virtualcollection.js +1 -1
- data/vendor/assets/javascripts/js_stack/marionette/marionette-1.1.0.js +2363 -0
- data/vendor/assets/javascripts/js_stack/marionette/marionette-1.4.0.js +2466 -0
- data/vendor/assets/javascripts/js_stack/marionette.js +1 -1
- data/vendor/assets/javascripts/js_stack/underscore.js +1276 -0
- data/vendor/assets/javascripts/js_stack.js +1 -0
- metadata +17 -36
@@ -1 +1 @@
|
|
1
|
-
//= require js_stack/backbone.pageable/backbone.pageable-1.
|
1
|
+
//= require js_stack/backbone.pageable/backbone.pageable-1.4.1
|
@@ -0,0 +1,634 @@
|
|
1
|
+
// Backbone.Validation v0.9.0
|
2
|
+
//
|
3
|
+
// Copyright (c) 2011-2013 Thomas Pedersen
|
4
|
+
// Distributed under MIT License
|
5
|
+
//
|
6
|
+
// Documentation and full license available at:
|
7
|
+
// http://thedersen.com/projects/backbone-validation
|
8
|
+
Backbone.Validation = (function(_){
|
9
|
+
'use strict';
|
10
|
+
|
11
|
+
// Default options
|
12
|
+
// ---------------
|
13
|
+
|
14
|
+
var defaultOptions = {
|
15
|
+
forceUpdate: false,
|
16
|
+
selector: 'name',
|
17
|
+
labelFormatter: 'sentenceCase',
|
18
|
+
valid: Function.prototype,
|
19
|
+
invalid: Function.prototype
|
20
|
+
};
|
21
|
+
|
22
|
+
|
23
|
+
// Helper functions
|
24
|
+
// ----------------
|
25
|
+
|
26
|
+
// Formatting functions used for formatting error messages
|
27
|
+
var formatFunctions = {
|
28
|
+
// Uses the configured label formatter to format the attribute name
|
29
|
+
// to make it more readable for the user
|
30
|
+
formatLabel: function(attrName, model) {
|
31
|
+
return defaultLabelFormatters[defaultOptions.labelFormatter](attrName, model);
|
32
|
+
},
|
33
|
+
|
34
|
+
// Replaces nummeric placeholders like {0} in a string with arguments
|
35
|
+
// passed to the function
|
36
|
+
format: function() {
|
37
|
+
var args = Array.prototype.slice.call(arguments),
|
38
|
+
text = args.shift();
|
39
|
+
return text.replace(/\{(\d+)\}/g, function(match, number) {
|
40
|
+
return typeof args[number] !== 'undefined' ? args[number] : match;
|
41
|
+
});
|
42
|
+
}
|
43
|
+
};
|
44
|
+
|
45
|
+
// Flattens an object
|
46
|
+
// eg:
|
47
|
+
//
|
48
|
+
// var o = {
|
49
|
+
// address: {
|
50
|
+
// street: 'Street',
|
51
|
+
// zip: 1234
|
52
|
+
// }
|
53
|
+
// };
|
54
|
+
//
|
55
|
+
// becomes:
|
56
|
+
//
|
57
|
+
// var o = {
|
58
|
+
// 'address.street': 'Street',
|
59
|
+
// 'address.zip': 1234
|
60
|
+
// };
|
61
|
+
var flatten = function (obj, into, prefix) {
|
62
|
+
into = into || {};
|
63
|
+
prefix = prefix || '';
|
64
|
+
|
65
|
+
_.each(obj, function(val, key) {
|
66
|
+
if(obj.hasOwnProperty(key)) {
|
67
|
+
if (val && typeof val === 'object' && !(
|
68
|
+
val instanceof Array ||
|
69
|
+
val instanceof Date ||
|
70
|
+
val instanceof RegExp ||
|
71
|
+
val instanceof Backbone.Model ||
|
72
|
+
val instanceof Backbone.Collection)
|
73
|
+
) {
|
74
|
+
flatten(val, into, prefix + key + '.');
|
75
|
+
}
|
76
|
+
else {
|
77
|
+
into[prefix + key] = val;
|
78
|
+
}
|
79
|
+
}
|
80
|
+
});
|
81
|
+
|
82
|
+
return into;
|
83
|
+
};
|
84
|
+
|
85
|
+
// Validation
|
86
|
+
// ----------
|
87
|
+
|
88
|
+
var Validation = (function(){
|
89
|
+
|
90
|
+
// Returns an object with undefined properties for all
|
91
|
+
// attributes on the model that has defined one or more
|
92
|
+
// validation rules.
|
93
|
+
var getValidatedAttrs = function(model) {
|
94
|
+
return _.reduce(_.keys(_.result(model, 'validation') || {}), function(memo, key) {
|
95
|
+
memo[key] = void 0;
|
96
|
+
return memo;
|
97
|
+
}, {});
|
98
|
+
};
|
99
|
+
|
100
|
+
// Looks on the model for validations for a specified
|
101
|
+
// attribute. Returns an array of any validators defined,
|
102
|
+
// or an empty array if none is defined.
|
103
|
+
var getValidators = function(model, attr) {
|
104
|
+
var attrValidationSet = model.validation ? _.result(model, 'validation')[attr] || {} : {};
|
105
|
+
|
106
|
+
// If the validator is a function or a string, wrap it in a function validator
|
107
|
+
if (_.isFunction(attrValidationSet) || _.isString(attrValidationSet)) {
|
108
|
+
attrValidationSet = {
|
109
|
+
fn: attrValidationSet
|
110
|
+
};
|
111
|
+
}
|
112
|
+
|
113
|
+
// Stick the validator object into an array
|
114
|
+
if(!_.isArray(attrValidationSet)) {
|
115
|
+
attrValidationSet = [attrValidationSet];
|
116
|
+
}
|
117
|
+
|
118
|
+
// Reduces the array of validators into a new array with objects
|
119
|
+
// with a validation method to call, the value to validate against
|
120
|
+
// and the specified error message, if any
|
121
|
+
return _.reduce(attrValidationSet, function(memo, attrValidation) {
|
122
|
+
_.each(_.without(_.keys(attrValidation), 'msg'), function(validator) {
|
123
|
+
memo.push({
|
124
|
+
fn: defaultValidators[validator],
|
125
|
+
val: attrValidation[validator],
|
126
|
+
msg: attrValidation.msg
|
127
|
+
});
|
128
|
+
});
|
129
|
+
return memo;
|
130
|
+
}, []);
|
131
|
+
};
|
132
|
+
|
133
|
+
// Validates an attribute against all validators defined
|
134
|
+
// for that attribute. If one or more errors are found,
|
135
|
+
// the first error message is returned.
|
136
|
+
// If the attribute is valid, an empty string is returned.
|
137
|
+
var validateAttr = function(model, attr, value, computed) {
|
138
|
+
// Reduces the array of validators to an error message by
|
139
|
+
// applying all the validators and returning the first error
|
140
|
+
// message, if any.
|
141
|
+
return _.reduce(getValidators(model, attr), function(memo, validator){
|
142
|
+
// Pass the format functions plus the default
|
143
|
+
// validators as the context to the validator
|
144
|
+
var ctx = _.extend({}, formatFunctions, defaultValidators),
|
145
|
+
result = validator.fn.call(ctx, value, attr, validator.val, model, computed);
|
146
|
+
|
147
|
+
if(result === false || memo === false) {
|
148
|
+
return false;
|
149
|
+
}
|
150
|
+
if (result && !memo) {
|
151
|
+
return _.result(validator, 'msg') || result;
|
152
|
+
}
|
153
|
+
return memo;
|
154
|
+
}, '');
|
155
|
+
};
|
156
|
+
|
157
|
+
// Loops through the model's attributes and validates them all.
|
158
|
+
// Returns and object containing names of invalid attributes
|
159
|
+
// as well as error messages.
|
160
|
+
var validateModel = function(model, attrs) {
|
161
|
+
var error,
|
162
|
+
invalidAttrs = {},
|
163
|
+
isValid = true,
|
164
|
+
computed = _.clone(attrs),
|
165
|
+
flattened = flatten(attrs);
|
166
|
+
|
167
|
+
_.each(flattened, function(val, attr) {
|
168
|
+
error = validateAttr(model, attr, val, computed);
|
169
|
+
if (error) {
|
170
|
+
invalidAttrs[attr] = error;
|
171
|
+
isValid = false;
|
172
|
+
}
|
173
|
+
});
|
174
|
+
|
175
|
+
return {
|
176
|
+
invalidAttrs: invalidAttrs,
|
177
|
+
isValid: isValid
|
178
|
+
};
|
179
|
+
};
|
180
|
+
|
181
|
+
// Contains the methods that are mixed in on the model when binding
|
182
|
+
var mixin = function(view, options) {
|
183
|
+
return {
|
184
|
+
|
185
|
+
// Check whether or not a value, or a hash of values
|
186
|
+
// passes validation without updating the model
|
187
|
+
preValidate: function(attr, value) {
|
188
|
+
var self = this,
|
189
|
+
result = {},
|
190
|
+
error;
|
191
|
+
|
192
|
+
if(_.isObject(attr)){
|
193
|
+
_.each(attr, function(value, key) {
|
194
|
+
error = self.preValidate(key, value);
|
195
|
+
if(error){
|
196
|
+
result[key] = error;
|
197
|
+
}
|
198
|
+
});
|
199
|
+
|
200
|
+
return _.isEmpty(result) ? undefined : result;
|
201
|
+
}
|
202
|
+
else {
|
203
|
+
return validateAttr(this, attr, value, _.extend({}, this.attributes));
|
204
|
+
}
|
205
|
+
},
|
206
|
+
|
207
|
+
// Check to see if an attribute, an array of attributes or the
|
208
|
+
// entire model is valid. Passing true will force a validation
|
209
|
+
// of the model.
|
210
|
+
isValid: function(option) {
|
211
|
+
var flattened = flatten(this.attributes);
|
212
|
+
|
213
|
+
if(_.isString(option)){
|
214
|
+
return !validateAttr(this, option, flattened[option], _.extend({}, this.attributes));
|
215
|
+
}
|
216
|
+
if(_.isArray(option)){
|
217
|
+
return _.reduce(option, function(memo, attr) {
|
218
|
+
return memo && !validateAttr(this, attr, flattened[attr], _.extend({}, this.attributes));
|
219
|
+
}, true, this);
|
220
|
+
}
|
221
|
+
if(option === true) {
|
222
|
+
this.validate();
|
223
|
+
}
|
224
|
+
return this.validation ? this._isValid : true;
|
225
|
+
},
|
226
|
+
|
227
|
+
// This is called by Backbone when it needs to perform validation.
|
228
|
+
// You can call it manually without any parameters to validate the
|
229
|
+
// entire model.
|
230
|
+
validate: function(attrs, setOptions){
|
231
|
+
var model = this,
|
232
|
+
validateAll = !attrs,
|
233
|
+
opt = _.extend({}, options, setOptions),
|
234
|
+
validatedAttrs = getValidatedAttrs(model),
|
235
|
+
allAttrs = _.extend({}, validatedAttrs, model.attributes, attrs),
|
236
|
+
changedAttrs = flatten(attrs || allAttrs),
|
237
|
+
|
238
|
+
result = validateModel(model, allAttrs);
|
239
|
+
|
240
|
+
model._isValid = result.isValid;
|
241
|
+
|
242
|
+
// After validation is performed, loop through all validated attributes
|
243
|
+
// and call the valid callbacks so the view is updated.
|
244
|
+
_.each(validatedAttrs, function(val, attr){
|
245
|
+
var invalid = result.invalidAttrs.hasOwnProperty(attr);
|
246
|
+
if(!invalid){
|
247
|
+
opt.valid(view, attr, opt.selector);
|
248
|
+
}
|
249
|
+
});
|
250
|
+
|
251
|
+
// After validation is performed, loop through all validated and changed attributes
|
252
|
+
// and call the invalid callback so the view is updated.
|
253
|
+
_.each(validatedAttrs, function(val, attr){
|
254
|
+
var invalid = result.invalidAttrs.hasOwnProperty(attr),
|
255
|
+
changed = changedAttrs.hasOwnProperty(attr);
|
256
|
+
|
257
|
+
if(invalid && (changed || validateAll)){
|
258
|
+
opt.invalid(view, attr, result.invalidAttrs[attr], opt.selector);
|
259
|
+
}
|
260
|
+
});
|
261
|
+
|
262
|
+
// Trigger validated events.
|
263
|
+
// Need to defer this so the model is actually updated before
|
264
|
+
// the event is triggered.
|
265
|
+
_.defer(function() {
|
266
|
+
model.trigger('validated', model._isValid, model, result.invalidAttrs);
|
267
|
+
model.trigger('validated:' + (model._isValid ? 'valid' : 'invalid'), model, result.invalidAttrs);
|
268
|
+
});
|
269
|
+
|
270
|
+
// Return any error messages to Backbone, unless the forceUpdate flag is set.
|
271
|
+
// Then we do not return anything and fools Backbone to believe the validation was
|
272
|
+
// a success. That way Backbone will update the model regardless.
|
273
|
+
if (!opt.forceUpdate && _.intersection(_.keys(result.invalidAttrs), _.keys(changedAttrs)).length > 0) {
|
274
|
+
return result.invalidAttrs;
|
275
|
+
}
|
276
|
+
}
|
277
|
+
};
|
278
|
+
};
|
279
|
+
|
280
|
+
// Helper to mix in validation on a model
|
281
|
+
var bindModel = function(view, model, options) {
|
282
|
+
_.extend(model, mixin(view, options));
|
283
|
+
};
|
284
|
+
|
285
|
+
// Removes the methods added to a model
|
286
|
+
var unbindModel = function(model) {
|
287
|
+
delete model.validate;
|
288
|
+
delete model.preValidate;
|
289
|
+
delete model.isValid;
|
290
|
+
};
|
291
|
+
|
292
|
+
// Mix in validation on a model whenever a model is
|
293
|
+
// added to a collection
|
294
|
+
var collectionAdd = function(model) {
|
295
|
+
bindModel(this.view, model, this.options);
|
296
|
+
};
|
297
|
+
|
298
|
+
// Remove validation from a model whenever a model is
|
299
|
+
// removed from a collection
|
300
|
+
var collectionRemove = function(model) {
|
301
|
+
unbindModel(model);
|
302
|
+
};
|
303
|
+
|
304
|
+
// Returns the public methods on Backbone.Validation
|
305
|
+
return {
|
306
|
+
|
307
|
+
// Current version of the library
|
308
|
+
version: '0.9.0',
|
309
|
+
|
310
|
+
// Called to configure the default options
|
311
|
+
configure: function(options) {
|
312
|
+
_.extend(defaultOptions, options);
|
313
|
+
},
|
314
|
+
|
315
|
+
// Hooks up validation on a view with a model
|
316
|
+
// or collection
|
317
|
+
bind: function(view, options) {
|
318
|
+
options = _.extend({}, defaultOptions, defaultCallbacks, options);
|
319
|
+
|
320
|
+
var model = options.model || view.model,
|
321
|
+
collection = options.collection || view.collection;
|
322
|
+
|
323
|
+
if(typeof model === 'undefined' && typeof collection === 'undefined'){
|
324
|
+
throw 'Before you execute the binding your view must have a model or a collection.\n' +
|
325
|
+
'See http://thedersen.com/projects/backbone-validation/#using-form-model-validation for more information.';
|
326
|
+
}
|
327
|
+
|
328
|
+
if(model) {
|
329
|
+
bindModel(view, model, options);
|
330
|
+
}
|
331
|
+
else if(collection) {
|
332
|
+
collection.each(function(model){
|
333
|
+
bindModel(view, model, options);
|
334
|
+
});
|
335
|
+
collection.bind('add', collectionAdd, {view: view, options: options});
|
336
|
+
collection.bind('remove', collectionRemove);
|
337
|
+
}
|
338
|
+
},
|
339
|
+
|
340
|
+
// Removes validation from a view with a model
|
341
|
+
// or collection
|
342
|
+
unbind: function(view, options) {
|
343
|
+
options = _.extend({}, options);
|
344
|
+
var model = options.model || view.model,
|
345
|
+
collection = options.collection || view.collection;
|
346
|
+
|
347
|
+
if(model) {
|
348
|
+
unbindModel(model);
|
349
|
+
}
|
350
|
+
if(collection) {
|
351
|
+
collection.each(function(model){
|
352
|
+
unbindModel(model);
|
353
|
+
});
|
354
|
+
collection.unbind('add', collectionAdd);
|
355
|
+
collection.unbind('remove', collectionRemove);
|
356
|
+
}
|
357
|
+
},
|
358
|
+
|
359
|
+
// Used to extend the Backbone.Model.prototype
|
360
|
+
// with validation
|
361
|
+
mixin: mixin(null, defaultOptions)
|
362
|
+
};
|
363
|
+
}());
|
364
|
+
|
365
|
+
|
366
|
+
// Callbacks
|
367
|
+
// ---------
|
368
|
+
|
369
|
+
var defaultCallbacks = Validation.callbacks = {
|
370
|
+
|
371
|
+
// Gets called when a previously invalid field in the
|
372
|
+
// view becomes valid. Removes any error message.
|
373
|
+
// Should be overridden with custom functionality.
|
374
|
+
valid: function(view, attr, selector) {
|
375
|
+
view.$('[' + selector + '~="' + attr + '"]')
|
376
|
+
.removeClass('invalid')
|
377
|
+
.removeAttr('data-error');
|
378
|
+
},
|
379
|
+
|
380
|
+
// Gets called when a field in the view becomes invalid.
|
381
|
+
// Adds a error message.
|
382
|
+
// Should be overridden with custom functionality.
|
383
|
+
invalid: function(view, attr, error, selector) {
|
384
|
+
view.$('[' + selector + '~="' + attr + '"]')
|
385
|
+
.addClass('invalid')
|
386
|
+
.attr('data-error', error);
|
387
|
+
}
|
388
|
+
};
|
389
|
+
|
390
|
+
|
391
|
+
// Patterns
|
392
|
+
// --------
|
393
|
+
|
394
|
+
var defaultPatterns = Validation.patterns = {
|
395
|
+
// Matches any digit(s) (i.e. 0-9)
|
396
|
+
digits: /^\d+$/,
|
397
|
+
|
398
|
+
// Matches any number (e.g. 100.000)
|
399
|
+
number: /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/,
|
400
|
+
|
401
|
+
// Matches a valid email address (e.g. mail@example.com)
|
402
|
+
email: /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i,
|
403
|
+
|
404
|
+
// Mathes any valid url (e.g. http://www.xample.com)
|
405
|
+
url: /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i
|
406
|
+
};
|
407
|
+
|
408
|
+
|
409
|
+
// Error messages
|
410
|
+
// --------------
|
411
|
+
|
412
|
+
// Error message for the build in validators.
|
413
|
+
// {x} gets swapped out with arguments form the validator.
|
414
|
+
var defaultMessages = Validation.messages = {
|
415
|
+
required: '{0} is required',
|
416
|
+
acceptance: '{0} must be accepted',
|
417
|
+
min: '{0} must be greater than or equal to {1}',
|
418
|
+
max: '{0} must be less than or equal to {1}',
|
419
|
+
range: '{0} must be between {1} and {2}',
|
420
|
+
length: '{0} must be {1} characters',
|
421
|
+
minLength: '{0} must be at least {1} characters',
|
422
|
+
maxLength: '{0} must be at most {1} characters',
|
423
|
+
rangeLength: '{0} must be between {1} and {2} characters',
|
424
|
+
oneOf: '{0} must be one of: {1}',
|
425
|
+
equalTo: '{0} must be the same as {1}',
|
426
|
+
digits: '{0} must only contain digits',
|
427
|
+
number: '{0} must be a number',
|
428
|
+
email: '{0} must be a valid email',
|
429
|
+
url: '{0} must be a valid url',
|
430
|
+
inlinePattern: '{0} is invalid'
|
431
|
+
};
|
432
|
+
|
433
|
+
// Label formatters
|
434
|
+
// ----------------
|
435
|
+
|
436
|
+
// Label formatters are used to convert the attribute name
|
437
|
+
// to a more human friendly label when using the built in
|
438
|
+
// error messages.
|
439
|
+
// Configure which one to use with a call to
|
440
|
+
//
|
441
|
+
// Backbone.Validation.configure({
|
442
|
+
// labelFormatter: 'label'
|
443
|
+
// });
|
444
|
+
var defaultLabelFormatters = Validation.labelFormatters = {
|
445
|
+
|
446
|
+
// Returns the attribute name with applying any formatting
|
447
|
+
none: function(attrName) {
|
448
|
+
return attrName;
|
449
|
+
},
|
450
|
+
|
451
|
+
// Converts attributeName or attribute_name to Attribute name
|
452
|
+
sentenceCase: function(attrName) {
|
453
|
+
return attrName.replace(/(?:^\w|[A-Z]|\b\w)/g, function(match, index) {
|
454
|
+
return index === 0 ? match.toUpperCase() : ' ' + match.toLowerCase();
|
455
|
+
}).replace(/_/g, ' ');
|
456
|
+
},
|
457
|
+
|
458
|
+
// Looks for a label configured on the model and returns it
|
459
|
+
//
|
460
|
+
// var Model = Backbone.Model.extend({
|
461
|
+
// validation: {
|
462
|
+
// someAttribute: {
|
463
|
+
// required: true
|
464
|
+
// }
|
465
|
+
// },
|
466
|
+
//
|
467
|
+
// labels: {
|
468
|
+
// someAttribute: 'Custom label'
|
469
|
+
// }
|
470
|
+
// });
|
471
|
+
label: function(attrName, model) {
|
472
|
+
return (model.labels && model.labels[attrName]) || defaultLabelFormatters.sentenceCase(attrName, model);
|
473
|
+
}
|
474
|
+
};
|
475
|
+
|
476
|
+
|
477
|
+
// Built in validators
|
478
|
+
// -------------------
|
479
|
+
|
480
|
+
var defaultValidators = Validation.validators = (function(){
|
481
|
+
// Use native trim when defined
|
482
|
+
var trim = String.prototype.trim ?
|
483
|
+
function(text) {
|
484
|
+
return text === null ? '' : String.prototype.trim.call(text);
|
485
|
+
} :
|
486
|
+
function(text) {
|
487
|
+
var trimLeft = /^\s+/,
|
488
|
+
trimRight = /\s+$/;
|
489
|
+
|
490
|
+
return text === null ? '' : text.toString().replace(trimLeft, '').replace(trimRight, '');
|
491
|
+
};
|
492
|
+
|
493
|
+
// Determines whether or not a value is a number
|
494
|
+
var isNumber = function(value){
|
495
|
+
return _.isNumber(value) || (_.isString(value) && value.match(defaultPatterns.number));
|
496
|
+
};
|
497
|
+
|
498
|
+
// Determines whether or not a value is empty
|
499
|
+
var hasValue = function(value) {
|
500
|
+
return !(_.isNull(value) || _.isUndefined(value) || (_.isString(value) && trim(value) === '') || (_.isArray(value) && _.isEmpty(value)));
|
501
|
+
};
|
502
|
+
|
503
|
+
return {
|
504
|
+
// Function validator
|
505
|
+
// Lets you implement a custom function used for validation
|
506
|
+
fn: function(value, attr, fn, model, computed) {
|
507
|
+
if(_.isString(fn)){
|
508
|
+
fn = model[fn];
|
509
|
+
}
|
510
|
+
return fn.call(model, value, attr, computed);
|
511
|
+
},
|
512
|
+
|
513
|
+
// Required validator
|
514
|
+
// Validates if the attribute is required or not
|
515
|
+
// This can be specified as either a boolean value or a function that returns a boolean value
|
516
|
+
required: function(value, attr, required, model, computed) {
|
517
|
+
var isRequired = _.isFunction(required) ? required.call(model, value, attr, computed) : required;
|
518
|
+
if(!isRequired && !hasValue(value)) {
|
519
|
+
return false; // overrides all other validators
|
520
|
+
}
|
521
|
+
if (isRequired && !hasValue(value)) {
|
522
|
+
return this.format(defaultMessages.required, this.formatLabel(attr, model));
|
523
|
+
}
|
524
|
+
},
|
525
|
+
|
526
|
+
// Acceptance validator
|
527
|
+
// Validates that something has to be accepted, e.g. terms of use
|
528
|
+
// `true` or 'true' are valid
|
529
|
+
acceptance: function(value, attr, accept, model) {
|
530
|
+
if(value !== 'true' && (!_.isBoolean(value) || value === false)) {
|
531
|
+
return this.format(defaultMessages.acceptance, this.formatLabel(attr, model));
|
532
|
+
}
|
533
|
+
},
|
534
|
+
|
535
|
+
// Min validator
|
536
|
+
// Validates that the value has to be a number and equal to or greater than
|
537
|
+
// the min value specified
|
538
|
+
min: function(value, attr, minValue, model) {
|
539
|
+
if (!isNumber(value) || value < minValue) {
|
540
|
+
return this.format(defaultMessages.min, this.formatLabel(attr, model), minValue);
|
541
|
+
}
|
542
|
+
},
|
543
|
+
|
544
|
+
// Max validator
|
545
|
+
// Validates that the value has to be a number and equal to or less than
|
546
|
+
// the max value specified
|
547
|
+
max: function(value, attr, maxValue, model) {
|
548
|
+
if (!isNumber(value) || value > maxValue) {
|
549
|
+
return this.format(defaultMessages.max, this.formatLabel(attr, model), maxValue);
|
550
|
+
}
|
551
|
+
},
|
552
|
+
|
553
|
+
// Range validator
|
554
|
+
// Validates that the value has to be a number and equal to or between
|
555
|
+
// the two numbers specified
|
556
|
+
range: function(value, attr, range, model) {
|
557
|
+
if(!isNumber(value) || value < range[0] || value > range[1]) {
|
558
|
+
return this.format(defaultMessages.range, this.formatLabel(attr, model), range[0], range[1]);
|
559
|
+
}
|
560
|
+
},
|
561
|
+
|
562
|
+
// Length validator
|
563
|
+
// Validates that the value has to be a string with length equal to
|
564
|
+
// the length value specified
|
565
|
+
length: function(value, attr, length, model) {
|
566
|
+
if (!_.isString(value) || value.length !== length) {
|
567
|
+
return this.format(defaultMessages.length, this.formatLabel(attr, model), length);
|
568
|
+
}
|
569
|
+
},
|
570
|
+
|
571
|
+
// Min length validator
|
572
|
+
// Validates that the value has to be a string with length equal to or greater than
|
573
|
+
// the min length value specified
|
574
|
+
minLength: function(value, attr, minLength, model) {
|
575
|
+
if (!_.isString(value) || value.length < minLength) {
|
576
|
+
return this.format(defaultMessages.minLength, this.formatLabel(attr, model), minLength);
|
577
|
+
}
|
578
|
+
},
|
579
|
+
|
580
|
+
// Max length validator
|
581
|
+
// Validates that the value has to be a string with length equal to or less than
|
582
|
+
// the max length value specified
|
583
|
+
maxLength: function(value, attr, maxLength, model) {
|
584
|
+
if (!_.isString(value) || value.length > maxLength) {
|
585
|
+
return this.format(defaultMessages.maxLength, this.formatLabel(attr, model), maxLength);
|
586
|
+
}
|
587
|
+
},
|
588
|
+
|
589
|
+
// Range length validator
|
590
|
+
// Validates that the value has to be a string and equal to or between
|
591
|
+
// the two numbers specified
|
592
|
+
rangeLength: function(value, attr, range, model) {
|
593
|
+
if (!_.isString(value) || value.length < range[0] || value.length > range[1]) {
|
594
|
+
return this.format(defaultMessages.rangeLength, this.formatLabel(attr, model), range[0], range[1]);
|
595
|
+
}
|
596
|
+
},
|
597
|
+
|
598
|
+
// One of validator
|
599
|
+
// Validates that the value has to be equal to one of the elements in
|
600
|
+
// the specified array. Case sensitive matching
|
601
|
+
oneOf: function(value, attr, values, model) {
|
602
|
+
if(!_.include(values, value)){
|
603
|
+
return this.format(defaultMessages.oneOf, this.formatLabel(attr, model), values.join(', '));
|
604
|
+
}
|
605
|
+
},
|
606
|
+
|
607
|
+
// Equal to validator
|
608
|
+
// Validates that the value has to be equal to the value of the attribute
|
609
|
+
// with the name specified
|
610
|
+
equalTo: function(value, attr, equalTo, model, computed) {
|
611
|
+
if(value !== computed[equalTo]) {
|
612
|
+
return this.format(defaultMessages.equalTo, this.formatLabel(attr, model), this.formatLabel(equalTo, model));
|
613
|
+
}
|
614
|
+
},
|
615
|
+
|
616
|
+
// Pattern validator
|
617
|
+
// Validates that the value has to match the pattern specified.
|
618
|
+
// Can be a regular expression or the name of one of the built in patterns
|
619
|
+
pattern: function(value, attr, pattern, model) {
|
620
|
+
if (!hasValue(value) || !value.toString().match(defaultPatterns[pattern] || pattern)) {
|
621
|
+
return this.format(defaultMessages[pattern] || defaultMessages.inlinePattern, this.formatLabel(attr, model), pattern);
|
622
|
+
}
|
623
|
+
}
|
624
|
+
};
|
625
|
+
}());
|
626
|
+
|
627
|
+
// Set the correct context for all validators
|
628
|
+
// when used from within a method validator
|
629
|
+
_.each(defaultValidators, function(validator, key){
|
630
|
+
defaultValidators[key] = _.bind(defaultValidators[key], _.extend({}, formatFunctions, defaultValidators));
|
631
|
+
});
|
632
|
+
|
633
|
+
return Validation;
|
634
|
+
}(_));
|
@@ -1 +1 @@
|
|
1
|
-
//= require js_stack/backbone.validation/backbone.validation-0.
|
1
|
+
//= require js_stack/backbone.validation/backbone.validation-0.9.0
|