[Pkg-javascript-commits] [backbone] 36/101: more tests for collection, 0 is a valid id.
Jonas Smedegaard
js at moszumanska.debian.org
Sat May 3 16:58:26 UTC 2014
This is an automated email from the git hooks/post-receive script.
js pushed a commit to tag 0.1.0
in repository backbone.
commit 6009b8d1ab3673cd25db6c988960d6bd6fc90ded
Author: Jeremy Ashkenas <jashkenas at gmail.com>
Date: Wed Oct 6 13:18:32 2010 -0400
more tests for collection, 0 is a valid id.
---
backbone.js | 22 ++++++++++------------
test/collection.js | 41 ++++++++++++++++++++++++++++++++++-------
2 files changed, 44 insertions(+), 19 deletions(-)
diff --git a/backbone.js b/backbone.js
index bddb41f..c0fa477 100644
--- a/backbone.js
+++ b/backbone.js
@@ -85,7 +85,7 @@
var list = calls && calls[i ? 'all' : ev];
if (!list) continue;
for (var j = 0, l = list.length; j < l; j++) {
- list[j].apply(this, arguments);
+ list[j].apply(this, _.rest(arguments));
}
}
return this;
@@ -164,7 +164,7 @@
if (!attrs) return this;
attrs = attrs._attributes || attrs;
var now = this._attributes;
- if (attrs.id) {
+ if ('id' in attrs) {
this.id = attrs.id;
if (this.collection) this.resource = this.collection.resource + '/' + this.id;
}
@@ -280,7 +280,7 @@
this.comparator = options.comparator;
delete options.comparator;
}
- this._boundOnModelEvent = _.bind(this._onModelEvent, this);
+ this._boundOnModelChange = _.bind(this._onModelChange, this);
this._initialize();
if (models) this.refresh(models,true);
};
@@ -339,7 +339,7 @@
model.collection = this;
var index = this.comparator ? this.sortedIndex(model, this.comparator) : this.length;
this.models.splice(index, 0, model);
- model.bind('all', this._boundOnModelEvent);
+ model.bind('change', this._boundOnModelChange);
this.length++;
if (!silent) this.trigger('add', model);
return model;
@@ -361,7 +361,7 @@
delete this._byCid[model.cid];
delete model.collection;
this.models.splice(this.indexOf(model), 1);
- model.unbind('all', this._boundOnModelEvent);
+ model.unbind('change', this._boundOnModelChange);
this.length--;
if (!silent) this.trigger('remove', model);
return model;
@@ -418,14 +418,12 @@
// Internal method called every time a model in the set fires an event.
// Sets need to update their indexes when models change ids.
- _onModelEvent : function(ev, model) {
- if (ev == 'change') {
- if (model.hasChanged('id')) {
- delete this._byId[model.formerValue('id')];
- this._byId[model.id] = model;
- }
- this.trigger('change', model);
+ _onModelChange : function(model) {
+ if (model.hasChanged('id')) {
+ delete this._byId[model.formerValue('id')];
+ this._byId[model.id] = model;
}
+ this.trigger('change', model);
},
// Inspect.
diff --git a/test/collection.js b/test/collection.js
index ccb9159..2502b5e 100644
--- a/test/collection.js
+++ b/test/collection.js
@@ -2,18 +2,45 @@ $(document).ready(function() {
module("Backbone collections");
- test("collections: simple", function() {
- a = new Backbone.Model({label: 'a'});
- b = new Backbone.Model({label: 'b'});
- c = new Backbone.Model({label: 'c'});
- d = new Backbone.Model({label: 'd'});
- col = new Backbone.Collection([a,b,c,d]);
+ var a = new Backbone.Model({id: 4, label: 'a'});
+ var b = new Backbone.Model({id: 3, label: 'b'});
+ var c = new Backbone.Model({id: 2, label: 'c'});
+ var d = new Backbone.Model({id: 1, label: 'd'});
+ var col = new Backbone.Collection([a,b,c,d]);
+
+ test("collections: simple and sorted", function() {
equals(col.first(), a, "a should be first");
equals(col.last(), d, "d should be last");
+ col.comparator = function(model) { return model.id; };
+ col.sort();
+ equals(col.first(), d, "d should be first");
+ equals(col.last(), a, "a should be last");
+ equals(col.length, 4);
+ });
+
+ test("collections: get, getByCid", function() {
+ equals(col.get(1), d);
+ equals(col.get(3), b);
+ equals(col.getByCid(col.first().cid), col.first());
});
- test("collections: sorted", function() {
+ test("collections: getIds, getCids", function() {
+ equals(col.getIds().sort().join(' '), '1 2 3 4');
+ equals(col.getCids().sort().join(' '), 'c1 c2 c3 c4');
+ });
+
+ test("collections: at", function() {
+ equals(col.at(2), b);
+ });
+ test("collections: add", function() {
+ var added = null;
+ col.bind('add', function(model){ added = model.get('label'); });
+ var e = new Backbone.Model({id: 0, label : 'e'});
+ col.add(e);
+ equals(added, 'e');
+ equals(col.length, 5);
+ equals(col.first(), e);
});
});
\ No newline at end of file
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/backbone.git
More information about the Pkg-javascript-commits
mailing list