eac3349d by Adam Heath

Extracted out seen() support, made it a mixin.

1 parent 895de9bd
{
"directory": "src/lib"
}
.*.swp
.tmp/
bin/coverage/
dist/
node_modules/
src/lib/
.grunt/
_SpecRunner.html
This diff is collapsed. Click to expand it.
{
"name": "backbone-seen",
"version": "0.0.0",
"authors": [
"Adam Heath <doogie@brainfood.com>"
],
"main": [
"src/scripts/backbone-seen.js"
],
"private": true,
"ignore": [
"**/.*",
"node_modules",
"src/lib",
"test"
],
"dependencies": {
"underscore": "~1.6.0",
"backbone": "~1.1.0",
"requirejs": "~2.1.10"
}
}
{
"name": "backbone-seen",
"version": "0.0.0",
"main": [
"src/scripts/backbone-seen.js"
],
"dependencies": {
"underscore": "~1.6.0",
"backbone": "~1.1.0",
"requirejs": "~2.1.10"
},
"devDependencies": {
"bower-requirejs": "~0.9.2",
"grunt": "~0.4.1",
"grunt-contrib-copy": "~0.4.1",
"grunt-contrib-concat": "~0.3.0",
"grunt-contrib-uglify": "~0.2.0",
"grunt-contrib-jshint": "~0.7.0",
"grunt-contrib-cssmin": "~0.7.0",
"grunt-contrib-connect": "~0.5.0",
"grunt-contrib-clean": "~0.5.0",
"grunt-contrib-htmlmin": "~0.1.3",
"grunt-bower-install": "~0.7.0",
"grunt-contrib-imagemin": "~0.2.0",
"grunt-contrib-watch": "~0.5.2",
"grunt-rev": "~0.1.0",
"grunt-autoprefixer": "~0.5.0",
"grunt-usemin": "~0.1.10",
"grunt-mocha": "~0.4.0",
"grunt-newer": "~0.6.0",
"grunt-svgmin": "~0.2.0",
"grunt-concurrent": "~0.4.0",
"load-grunt-tasks": "~0.2.0",
"time-grunt": "~0.2.0",
"jshint-stylish": "~0.1.3",
"grunt-contrib-requirejs": "~0.4.0",
"grunt-bower-requirejs": "~0.8.4",
"grunt-template-jasmine-istanbul": "~0.2.6",
"grunt-template-jasmine-requirejs": "~0.1.10",
"grunt-contrib-jasmine": "~0.5.3"
},
"engines": {
"node": ">=0.8.0"
}
}
define(
[
'underscore',
],
function(
_
) {
'use strict';
var seen = function(attrName, wasSeen) {
if (this._seen === undefined) {
this._seen = {};
}
if (attrName === undefined) {
return _.keys(_.clone(this._seen));
}
if (wasSeen) {
this._seen[attrName] = true;
} else if (wasSeen === false) {
delete this._seen[attrName];
} else {
return this._seen[attrName];
}
return this;
};
return {
mixin: function(modelClass) {
modelClass.prototype.seen = seen;
return modelClass;
},
};
}
);
/* global require:true */
var require;
require = (function() {
'use strict';
var require = {
baseUrl: 'scripts',
shim: {
},
paths: {
backbone: '../lib/backbone/backbone',
underscore: '../lib/underscore/underscore'
}
};
return require;
})();
define([], {});
/* global require */
require(
[],
function() {
'use strict';
}
);
define(function(require) {
'use strict';
var BackboneSeen = require('backbone-seen');
var Backbone = require('backbone');
describe('BackboneSeen', function() {
it('exists', function() {
expect(BackboneSeen).toBeTruthy();
expect(Backbone.Model.prototype.seen).toBeUndefined();
});
});
describe('BackboneSeen', function() {
var Base, Parent, Child;
beforeEach(function() {
Base = Backbone.Model.extend();
Parent = Base.extend();
Child = Parent.extend();
});
it('mixin', function() {
expect(Base.prototype.seen).toBeUndefined();
expect(Parent.prototype.seen).toBeUndefined();
expect(Child.prototype.seen).toBeUndefined();
var base = new Base({a: '1'});
var parent = new Parent({a: '1'});
var child = new Child({b: 'c'});
expect(base.seen).toBeUndefined();
expect(parent.seen).toBeUndefined();
expect(child.seen).toBeUndefined();
Parent = BackboneSeen.mixin(Parent);
var seen = parent.seen;
expect(base.seen).toBeUndefined();
expect(seen).toEqual(jasmine.any(Function));
expect(child.seen).toBe(parent.seen);
});
it('seen', function() {
Parent = BackboneSeen.mixin(Parent);
var parent = new Parent({a: '1'});
expect(parent.seen()).toEqual([]);
expect(parent.seen('a')).toBe(undefined);
expect(parent.seen('a', true)).toBe(parent);
expect(parent.seen()).toEqual(['a']);
expect(parent.seen('a')).toBe(true);
expect(parent.seen('a', false)).toBe(parent);
expect(parent.seen()).toEqual([]);
expect(parent.seen('a')).toBe(undefined);
});
});
});