e4adb2bb by Adam Heath

New adapter which allows for indexing into a backbone collection.

1 parent 35204adc
.*.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 module = require('module');
var Backbone = require('backbone');
var rivets = require('rivets');
var separator = module.config().separator || ':';
var adapter = rivets.adapters[separator] = (function(Model, Collection, collectionEvents) {
var observableFactory = function observableFactory(eventMethodName) {
return function (obj, keypath, callback) {
var eventMethod = obj[eventMethodName];
if (obj instanceof Collection) {
eventMethod.call(obj, collectionEvents, callback);
} else if (obj instanceof Model) {
eventMethod.call(obj, keypath === '*' ? 'change' : ('change:' + keypath), callback);
if (keypath !== '*') {
var value = obj.get(keypath);
if (value instanceof Collection) {
value[eventMethodName].call(value, collectionEvents, callback);
}
}
}
};
};
return {
observe: observableFactory('on'),
unobserve: observableFactory('off'),
get: function get(obj, keypath) {
if (obj instanceof Model) {
if (keypath === '*') {
return obj.attributes;
}
} else if (obj instanceof Collection) {
if (keypath === '*') {
return obj.models;
}
} else {
return;
}
return obj.get(keypath);
},
set: function set(obj, keypath, value) {
if (obj instanceof Model) {
if (keypath === '*') {
obj.set(value);
} else {
obj.set(keypath, value);
}
} else if (obj instanceof Collection) {
if (keypath === '*') {
obj.set(value);
} else {
var existing = obj.get(keypath);
if (existing) {
obj.set(value, {at: obj.indexOf(existing)});
}
}
}
},
};
})(Backbone.Model, Backbone.Collection, 'add remove reset sort');
return adapter;
});
define(function(require) {
'use strict';
var $ = require('jquery');
window.jQuery = $;
var RivetsBackboneAdapterBrainfood = require('rivets-backbone-adapter-brainfood');
var _ = require('underscore');
var Backbone = require('backbone');
var rivets = require('rivets');
require('backbone-validation');
_.extend(Backbone.Model.prototype, Backbone.Validation.mixin);
//rivets.config.rootInterface = ':';
/* global console:false */
describe('RivetsBackboneAdapterBrainfood', function() {
it('returns undefined', function() {
expect(RivetsBackboneAdapterBrainfood).toBeDefined();
});
var ContactMech, ContactMechs;
beforeEach(function() {
ContactMech = Backbone.Model.extend({
defaults: {
contactMechId: '',
contactMechPurposeTypeId: '',
infoString: '',
address1: '',
},
});
ContactMechs = Backbone.Collection.extend({
modelId: function(attrs) {
return attrs.contactMechPurposeTypeId;
},
});
});
var contactMechs, model;
beforeEach(function() {
contactMechs = new ContactMechs([
new ContactMech({
contactMechPurposeTypeId: 'PRIMARY_EMAIL',
infoString: 'user@example.com',
}),
new ContactMech({
contactMechPurposeTypeId: 'PRIMARY_ADDRESS',
address1: '1234 Main St.',
}),
]);
model = new Backbone.Model({contactMechs: contactMechs, notBackbone: {}});
});
function buildTest(path, options) {
it(path, function() {
var inputElement = document.createElement('input');
inputElement.setAttribute('rv-value', path);
var view = rivets.bind(inputElement, model, {
formatters: {
toJSON: function(value) {
return value.toJSON();
},
jsonStringify: {
read: function(value) {
return JSON.stringify(value);
},
publish: function(value) {
return JSON.parse(value);
},
},
},
});
function getInputElementValue() {
var rawValue = inputElement.value;
if (options.parse) {
rawValue = options.parse(rawValue);
}
return rawValue;
}
function convertValue(value) {
if (typeof value === 'function') {
return value();
} else {
return value;
}
}
var value = convertValue(options.value);
expect(getInputElementValue()).toEqual(value);
if (options.get) {
var newValue = convertValue(options.newValue);
inputElement.value = newValue;
$(inputElement).trigger('input').trigger('change');
expect(options.get()).toEqual(newValue);
if (options.set) {
options.set(value);
expect(getInputElementValue()).toEqual(value);
}
} else if (options.set) {
}
});
}
buildTest(':contactMechs:PRIMARY_EMAIL:infoString', {
value: 'user@example.com',
newValue: '(user@example.com)',
get: function() {
return contactMechs.get('PRIMARY_EMAIL').get('infoString');
},
});
buildTest(':contactMechs:PRIMARY_ADDRESS:address1', {
value: '1234 Main St.',
newValue: '(1234 Main St.)',
get: function() {
return contactMechs.get('PRIMARY_ADDRESS').get('address1');
},
set: function(value) {
contactMechs.get('PRIMARY_ADDRESS').set('address1', value);
},
});
buildTest(':contactMechs:PRIMARY_EMAIL:*.infoString', {
value: 'user@example.com',
});
buildTest(':notBackbone:notValid', {
value: '',
});
//buildTest(':contactMechs:*.length', '2');
buildTest(':contactMechs:PRIMARY_EMAIL | toJSON | jsonStringify', {
value: function() {
var primaryEmail = contactMechs.get('PRIMARY_EMAIL');
return primaryEmail.toJSON();
},
parse: function(rawValue) {
return JSON.parse(rawValue);
},
});
/*
buildTest(':contactMechs:PRIMARY_EMAIL:* | jsonStringify', {
value: function() {
var primaryEmail = contactMechs.get('PRIMARY_EMAIL');
return primaryEmail.toJSON();
},
newValue: JSON.stringify({
infoString: 'someone@example.com',
}),
parse: function(rawValue) {
return JSON.parse(rawValue);
},
get: function() {
var primaryEmail = contactMechs.get('PRIMARY_EMAIL');
console.log('primaryEmail', JSON.stringify(primaryEmail.toJSON()));
return primaryEmail.toJSON();
},
});
*/
});
});