f567b300 by Adam Heath

New feature, full coverage, useful for setting a require class based on

the validation required setting.
1 parent e3b741c4
.*.swp
.tmp/
bin/coverage/
dist/
node_modules/
src/lib/
.grunt/
_SpecRunner.html
// Generated on 2014-02-06 using generator-webapp 0.4.7
/* global module */
// # Globbing
// for performance reasons we're only matching one level down:
// 'test/spec/{,*/}*.js'
// use this if you want to recursively match all subfolders:
// 'test/spec/**/*.js'
module.exports = function (grunt) {
/* global require */
'use strict';
var config = {};
config.base = 'src';
config.jshint = {
options: {
},
browserOptions: {
},
};
config.bower = {
directory: 'lib/bower',
};
config.jscs = {
options: {
validateIndentation: 4,
reporter: 'console',
maxErrors: -1,
},
};
config.jasmine = {
withCoverage: true,
};
var montyPython = require('grunt-monty-python')(grunt);
montyPython.createConfig(config);
};
{
"name": "rivets-backbone-is-required",
"version": "0.0.0",
"authors": [
"Adam Heath <doogie@brainfood.com>"
],
"main": [
"src/scripts/rivets-backbone-is-required.js"
],
"private": true,
"ignore": [
"**/.*",
"node_modules",
"src/lib"
],
"dependencies": {
"backbone": "",
"backbone-validation": "",
"jquery": "",
"requirejs": "",
"rivets": "",
"underscore": ""
},
"devDependencies": {
"rivets-backbone-adapter": ""
}
}
{
"name": "rivets-backbone-is-required",
"version": "0.0.0",
"main": [
"src/scripts/rivets-backbone-is-required.js"
],
"dependencies": {
"rivets": "",
"requirejs": ""
},
"devDependencies": {
"bower-requirejs": "~0.9.2",
"grunt": "~0",
"grunt-monty-python": "git+ssh://git@gitlab.brainfood.com:brainfood/grunt-monty-python.git"
},
"engines": {
"node": ">=0.8.0"
}
}
/* global require:true */
var require;
require = (function() {
'use strict';
var require = {
baseUrl: 'scripts',
config: {
'rivets-error-binder': {}
},
shim: {
bootstrap: {
deps: [
'jquery'
]
},
rivets: {
deps: [
'jquery'
]
}
},
paths: {
'backbone-validation': '../lib/bower/backbone-validation/dist/backbone-validation-amd',
backbone: '../lib/bower/backbone/backbone',
underscore: '../lib/bower/underscore/underscore',
sightglass: '../lib/bower/sightglass/index',
rivets: '../lib/bower/rivets/dist/rivets',
jquery: '../lib/bower/jquery/dist/jquery',
'rivets-backbone-adapter': '../lib/bower/rivets-backbone-adapter/rivets-backbone',
}
};
return require;
})();
/* global require */
require(
[],
function() {
'use strict';
}
);
define(function(require) {
'use strict';
var _ = require('underscore');
var rivets = require('rivets');
rivets.formatters['is-required'] = function(obj, fieldName) {
if (!obj) {
return false;
}
var validation = obj.validation;
if (!validation) {
return false;
}
var validators = validation[fieldName];
if (!validators) {
return false;
}
if(!_.isArray(validators)) {
validators = [validators];
}
var computed = obj.toJSON();
return _.filter(validators, function(validator) {
var required = validator.required;
if (_.isFunction(required)) {
return required.call(this, null, fieldName, computed);
} else {
return required;
}
}, obj).length > 0;
};
});
define(function(require) {
'use strict';
var $ = require('jquery');
window.jQuery = $;
var RivetsBackboneIsRequired = require('rivets-backbone-is-required');
var _ = require('underscore');
var Backbone = require('backbone');
var rivets = require('rivets');
require('backbone-validation');
require('rivets-backbone-adapter');
_.extend(Backbone.Model.prototype, Backbone.Validation.mixin);
//rivets.config.rootInterface = ':';
describe('RivetsBackboneIsRequired', function() {
it('returns undefined', function() {
expect(RivetsBackboneIsRequired).toBeUndefined();
});
var Person, Address;
beforeEach(function() {
Person = Backbone.Model.extend({
defaults: {
firstName: '',
middleName: '',
lastName: '',
},
validation: {
firstName: [
{required: true},
],
lastName: {
required: function() { return true; },
},
},
});
Address = Backbone.Model.extend({
defaults: {
address1: '',
},
});
});
var person, address, model;
beforeEach(function() {
person = new Person({
firstName: 'Adam',
lastName: 'Heath',
});
address = new Address();
model = new Backbone.Model({person: person, address: address});
});
function buildTest(objName, fieldName, wantedValue, isRequired) {
it(objName + ':' + fieldName + '=' + isRequired, function() {
var span = document.createElement('span');
span.setAttribute('rv-text', ':' + objName + ':' + fieldName);
span.setAttribute('rv-class-required', ':' + objName + ' | is-required \'' + fieldName + '\'');
//console.log('before bind', span.outerHTML);
var view = rivets.bind(span, model);
//console.log('after bind', span.outerHTML);
if (isRequired) {
expect(span.getAttribute('class')).toMatch(/\brequired\b/);
} else {
expect(span.getAttribute('class')).toBe(null);
}
});
}
buildTest('person', 'firstName', 'Adam', true);
buildTest('person', 'middleName', '', false);
buildTest('person', 'lastName', 'Heath', true);
buildTest('address', 'address1', '', false);
buildTest('missing', 'field', '', false);
});
});