Permalink
Browse files

Merge branch 'before_initialize'

Conflicts:
	test/test.js
  • Loading branch information...
2 parents b0c29c4 + bf93b9a commit dce92b2d0fa23f85bb3531873f3079660d06393d @saimonmoore saimonmoore committed Feb 2, 2012
Showing with 61 additions and 7 deletions.
  1. +4 −0 backbone.subset.js
  2. +1 −1 package.json
  3. +56 −6 test/test.js
View
@@ -31,6 +31,10 @@
this.parent().bind('all', this._proxyEvents);
+ if (this.beforeInitialize) {
+ this.beforeInitialize.apply(this, arguments);
+ }
+
this._reset();
this.reset(models || this.parent().models, {silent: true});
this.initialize.apply(this, arguments);
View
@@ -4,6 +4,6 @@
"version": "0.0.2",
"author": "Pau ramon <masylum@gmail.com>",
"repository": {"type": "git", "url": "git://github.com/masylum/Backbone.Subset.git"},
- "devDependencies": {"backbone": "*", "underscore": "*", "mocha": "*"},
+ "devDependencies": {"backbone": "0.5.3", "underscore": "*", "mocha": "*"},
"main": "./backbone.subset"
}
View
@@ -1,17 +1,17 @@
-/*global it, describe*/
+/*global it, describe, before*/
var _ = require('underscore')
, assert = require('assert')
, happened = {}
, Models = {}
, Collections = {}
// instances
- , tasks, archived_tasks;
+ , tasks, archived_tasks, project, project_tasks;
function inc(what) {
return function () {
happened[what]++;
- }
+ };
}
GLOBAL.Backbone = require('backbone');
@@ -26,6 +26,8 @@ Models.Task = Backbone.Model.extend({
}
});
+Models.Project = Backbone.Model.extend({});
+
Collections.Tasks = Backbone.Collection.extend({
model: Models.Task
, comparator: function (m) {
@@ -45,9 +47,28 @@ Collections.ArchivedTasks = Backbone.Subset.extend({
}
});
+Collections.ProjectTasks = Backbone.Subset.extend({
+ beforeInitialize: function (models, options) {
+ this.project = options.project;
+ }
+, parent: function () {
+ return tasks;
+ }
+, sieve: function (task) {
+ if (this.project) {
+ return this.project.id === task.get('project_id');
+ }
+ else {
+ return false;
+ }
+ }
+});
+
+
tasks = new Collections.Tasks();
archived_tasks = new Collections.ArchivedTasks();
+
// lengths
describe('Subset', function () {
it('has a `add` function that behaves like the `Collection` one + bubbling', function () {
@@ -73,7 +94,7 @@ describe('Subset', function () {
it('contains corrects ids', function () {
assert.deepEqual(tasks.pluck('id'), [0, 1, 2, 3]);
- assert.deepEqual(archived_tasks.pluck('id'), [3,1]);
+ assert.deepEqual(archived_tasks.pluck('id'), [3, 1]);
});
it('has a `get` function that behaves like the `Collection` one + bubbling', function () {
@@ -168,9 +189,9 @@ describe('Aggregated collections', function () {
assert.equal(happened.archived_tasks, 1);
assert.deepEqual(tasks.pluck('id'), [0, 1, 2]);
- assert.deepEqual(archived_tasks.pluck('id'), [2,1]);
+ assert.deepEqual(archived_tasks.pluck('id'), [2, 1]);
assert.deepEqual(_.pluck(tasks.models, 'cid'), ['c6', 'c7', 'c8']);
- assert.deepEqual(_.pluck(archived_tasks.models, 'cid'), ['c8','c7']);
+ assert.deepEqual(_.pluck(archived_tasks.models, 'cid'), ['c8', 'c7']);
happened = {archived_tasks: 0, tasks: 0, tasks_add: 0, archived_tasks_add: 0, tasks_remove: 0, archived_tasks_remove: 0};
tasks.unbind('add');
@@ -282,3 +303,32 @@ describe('Live updating subset membership', function() {
assert.equal(archived_tasks.length, 2);
});
});
+
+describe('Sieves dependant on an association', function () {
+ before(function () {
+ tasks.reset();
+ project = new Models.Project({id: 1});
+ project_tasks = new Collections.ProjectTasks([], {project: project});
+ });
+
+ it('it filters by association', function () {
+ happened = {tasks: 0, project_tasks: 0};
+ tasks.bind('add', inc('tasks'));
+ project_tasks.bind('add', inc('project_tasks'));
+
+ for (var i = 0; i < 4; i++) {
+ project_tasks.add({id: i, project_id: i % 2, order: i});
+ }
+
+ assert.equal(happened.tasks, 4);
+ assert.equal(happened.project_tasks, 2);
+
+ assert.equal(tasks.length, 4);
+ assert.equal(project_tasks.length, 2);
+ });
+
+ it('contains corrects ids', function () {
+ assert.deepEqual(tasks.pluck('id'), [0, 1, 2, 3]);
+ assert.deepEqual(project_tasks.pluck('id'), [1, 3]);
+ });
+});

0 comments on commit dce92b2

Please sign in to comment.