New feature, full coverage, useful for setting a require class based on
the validation required setting.
Showing
8 changed files
with
237 additions
and
0 deletions
.gitignore
0 → 100644
Gruntfile.js
0 → 100644
1 | // Generated on 2014-02-06 using generator-webapp 0.4.7 | ||
2 | /* global module */ | ||
3 | |||
4 | // # Globbing | ||
5 | // for performance reasons we're only matching one level down: | ||
6 | // 'test/spec/{,*/}*.js' | ||
7 | // use this if you want to recursively match all subfolders: | ||
8 | // 'test/spec/**/*.js' | ||
9 | |||
10 | module.exports = function (grunt) { | ||
11 | /* global require */ | ||
12 | 'use strict'; | ||
13 | |||
14 | var config = {}; | ||
15 | config.base = 'src'; | ||
16 | config.jshint = { | ||
17 | options: { | ||
18 | }, | ||
19 | browserOptions: { | ||
20 | }, | ||
21 | }; | ||
22 | config.bower = { | ||
23 | directory: 'lib/bower', | ||
24 | }; | ||
25 | config.jscs = { | ||
26 | options: { | ||
27 | validateIndentation: 4, | ||
28 | reporter: 'console', | ||
29 | maxErrors: -1, | ||
30 | }, | ||
31 | }; | ||
32 | config.jasmine = { | ||
33 | withCoverage: true, | ||
34 | }; | ||
35 | var montyPython = require('grunt-monty-python')(grunt); | ||
36 | montyPython.createConfig(config); | ||
37 | }; |
bower.json
0 → 100644
1 | { | ||
2 | "name": "rivets-backbone-is-required", | ||
3 | "version": "0.0.0", | ||
4 | "authors": [ | ||
5 | "Adam Heath <doogie@brainfood.com>" | ||
6 | ], | ||
7 | "main": [ | ||
8 | "src/scripts/rivets-backbone-is-required.js" | ||
9 | ], | ||
10 | "private": true, | ||
11 | "ignore": [ | ||
12 | "**/.*", | ||
13 | "node_modules", | ||
14 | "src/lib" | ||
15 | ], | ||
16 | "dependencies": { | ||
17 | "backbone": "", | ||
18 | "backbone-validation": "", | ||
19 | "jquery": "", | ||
20 | "requirejs": "", | ||
21 | "rivets": "", | ||
22 | "underscore": "" | ||
23 | }, | ||
24 | "devDependencies": { | ||
25 | "rivets-backbone-adapter": "" | ||
26 | } | ||
27 | } |
package.json
0 → 100644
1 | { | ||
2 | "name": "rivets-backbone-is-required", | ||
3 | "version": "0.0.0", | ||
4 | "main": [ | ||
5 | "src/scripts/rivets-backbone-is-required.js" | ||
6 | ], | ||
7 | "dependencies": { | ||
8 | "rivets": "", | ||
9 | "requirejs": "" | ||
10 | }, | ||
11 | "devDependencies": { | ||
12 | "bower-requirejs": "~0.9.2", | ||
13 | "grunt": "~0", | ||
14 | "grunt-monty-python": "git+ssh://git@gitlab.brainfood.com:brainfood/grunt-monty-python.git" | ||
15 | }, | ||
16 | "engines": { | ||
17 | "node": ">=0.8.0" | ||
18 | } | ||
19 | } | ||
20 |
src/scripts/config.js
0 → 100644
1 | /* global require:true */ | ||
2 | var require; | ||
3 | require = (function() { | ||
4 | 'use strict'; | ||
5 | |||
6 | var require = { | ||
7 | baseUrl: 'scripts', | ||
8 | config: { | ||
9 | 'rivets-error-binder': {} | ||
10 | }, | ||
11 | shim: { | ||
12 | bootstrap: { | ||
13 | deps: [ | ||
14 | 'jquery' | ||
15 | ] | ||
16 | }, | ||
17 | rivets: { | ||
18 | deps: [ | ||
19 | 'jquery' | ||
20 | ] | ||
21 | } | ||
22 | }, | ||
23 | paths: { | ||
24 | 'backbone-validation': '../lib/bower/backbone-validation/dist/backbone-validation-amd', | ||
25 | backbone: '../lib/bower/backbone/backbone', | ||
26 | underscore: '../lib/bower/underscore/underscore', | ||
27 | sightglass: '../lib/bower/sightglass/index', | ||
28 | rivets: '../lib/bower/rivets/dist/rivets', | ||
29 | jquery: '../lib/bower/jquery/dist/jquery', | ||
30 | 'rivets-backbone-adapter': '../lib/bower/rivets-backbone-adapter/rivets-backbone', | ||
31 | } | ||
32 | }; | ||
33 | |||
34 | return require; | ||
35 | })(); |
src/scripts/main.js
0 → 100644
src/scripts/rivets-backbone-is-required.js
0 → 100644
1 | define(function(require) { | ||
2 | 'use strict'; | ||
3 | var _ = require('underscore'); | ||
4 | var rivets = require('rivets'); | ||
5 | |||
6 | rivets.formatters['is-required'] = function(obj, fieldName) { | ||
7 | if (!obj) { | ||
8 | return false; | ||
9 | } | ||
10 | var validation = obj.validation; | ||
11 | if (!validation) { | ||
12 | return false; | ||
13 | } | ||
14 | var validators = validation[fieldName]; | ||
15 | if (!validators) { | ||
16 | return false; | ||
17 | } | ||
18 | if(!_.isArray(validators)) { | ||
19 | validators = [validators]; | ||
20 | } | ||
21 | var computed = obj.toJSON(); | ||
22 | return _.filter(validators, function(validator) { | ||
23 | var required = validator.required; | ||
24 | if (_.isFunction(required)) { | ||
25 | return required.call(this, null, fieldName, computed); | ||
26 | } else { | ||
27 | return required; | ||
28 | } | ||
29 | }, obj).length > 0; | ||
30 | }; | ||
31 | }); |
1 | define(function(require) { | ||
2 | 'use strict'; | ||
3 | |||
4 | var $ = require('jquery'); | ||
5 | window.jQuery = $; | ||
6 | var RivetsBackboneIsRequired = require('rivets-backbone-is-required'); | ||
7 | var _ = require('underscore'); | ||
8 | var Backbone = require('backbone'); | ||
9 | var rivets = require('rivets'); | ||
10 | require('backbone-validation'); | ||
11 | require('rivets-backbone-adapter'); | ||
12 | _.extend(Backbone.Model.prototype, Backbone.Validation.mixin); | ||
13 | //rivets.config.rootInterface = ':'; | ||
14 | |||
15 | describe('RivetsBackboneIsRequired', function() { | ||
16 | it('returns undefined', function() { | ||
17 | expect(RivetsBackboneIsRequired).toBeUndefined(); | ||
18 | }); | ||
19 | var Person, Address; | ||
20 | beforeEach(function() { | ||
21 | Person = Backbone.Model.extend({ | ||
22 | defaults: { | ||
23 | firstName: '', | ||
24 | middleName: '', | ||
25 | lastName: '', | ||
26 | }, | ||
27 | validation: { | ||
28 | firstName: [ | ||
29 | {required: true}, | ||
30 | ], | ||
31 | lastName: { | ||
32 | required: function() { return true; }, | ||
33 | }, | ||
34 | }, | ||
35 | }); | ||
36 | Address = Backbone.Model.extend({ | ||
37 | defaults: { | ||
38 | address1: '', | ||
39 | }, | ||
40 | }); | ||
41 | }); | ||
42 | var person, address, model; | ||
43 | beforeEach(function() { | ||
44 | person = new Person({ | ||
45 | firstName: 'Adam', | ||
46 | lastName: 'Heath', | ||
47 | }); | ||
48 | address = new Address(); | ||
49 | model = new Backbone.Model({person: person, address: address}); | ||
50 | }); | ||
51 | function buildTest(objName, fieldName, wantedValue, isRequired) { | ||
52 | it(objName + ':' + fieldName + '=' + isRequired, function() { | ||
53 | var span = document.createElement('span'); | ||
54 | span.setAttribute('rv-text', ':' + objName + ':' + fieldName); | ||
55 | span.setAttribute('rv-class-required', ':' + objName + ' | is-required \'' + fieldName + '\''); | ||
56 | //console.log('before bind', span.outerHTML); | ||
57 | var view = rivets.bind(span, model); | ||
58 | //console.log('after bind', span.outerHTML); | ||
59 | if (isRequired) { | ||
60 | expect(span.getAttribute('class')).toMatch(/\brequired\b/); | ||
61 | } else { | ||
62 | expect(span.getAttribute('class')).toBe(null); | ||
63 | } | ||
64 | }); | ||
65 | } | ||
66 | buildTest('person', 'firstName', 'Adam', true); | ||
67 | buildTest('person', 'middleName', '', false); | ||
68 | buildTest('person', 'lastName', 'Heath', true); | ||
69 | buildTest('address', 'address1', '', false); | ||
70 | buildTest('missing', 'field', '', false); | ||
71 | }); | ||
72 | }); |
-
Please register or sign in to post a comment