[Pkg-javascript-commits] [backbone] 40/101: finished initial round of tests for collection
Jonas Smedegaard
js at moszumanska.debian.org
Sat May 3 16:58:27 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 655ab7fa1c4cd46e35068d75ba19f6eee2662a03
Author: Jeremy Ashkenas <jashkenas at gmail.com>
Date: Wed Oct 6 13:45:05 2010 -0400
finished initial round of tests for collection
---
backbone.js | 40 ++++++++++++++++++++--------------------
test/collection.js | 6 +++++-
2 files changed, 25 insertions(+), 21 deletions(-)
diff --git a/backbone.js b/backbone.js
index 9206dfc..4a863ab 100644
--- a/backbone.js
+++ b/backbone.js
@@ -291,13 +291,9 @@
model : Backbone.Model,
- // Initialize or re-initialize all internal state. Called when the
- // collection is refreshed.
- _initialize : function(options) {
- this.length = 0;
- this.models = [];
- this._byId = {};
- this._byCid = {};
+ // Override this function to get convenient logging in the console.
+ toString : function() {
+ return 'Collection (' + this.length + " models)";
},
// Get a model from the set by id.
@@ -372,6 +368,16 @@
return model;
},
+ // Force the set to re-sort itself. You don't need to call this under normal
+ // circumstances, as the set will maintain sort order as each item is added.
+ sort : function(options) {
+ options || (options = {});
+ if (!this.comparator) throw new Error('Cannot sort a set without a comparator');
+ this.models = this.sortBy(this.comparator);
+ if (!options.silent) this.trigger('refresh');
+ return this;
+ },
+
// When you have more items than you want to add or remove individually,
// you can refresh the entire set with a new list of models, without firing
// any `added` or `removed` events. Fires `refreshed` when finished.
@@ -416,14 +422,13 @@
return model.save(null, {success : success, error : options.error});
},
- // Force the set to re-sort itself. You don't need to call this under normal
- // circumstances, as the set will maintain sort order as each item is added.
- sort : function(options) {
- options || (options = {});
- if (!this.comparator) throw new Error('Cannot sort a set without a comparator');
- this.models = this.sortBy(this.comparator);
- if (!options.silent) this.trigger('refresh');
- return this;
+ // Initialize or re-initialize all internal state. Called when the
+ // collection is refreshed.
+ _initialize : function(options) {
+ this.length = 0;
+ this.models = [];
+ this._byId = {};
+ this._byCid = {};
},
// Internal method called every time a model in the set fires an event.
@@ -434,11 +439,6 @@
this._byId[model.id] = model;
}
this.trigger('change', model);
- },
-
- // Inspect.
- toString : function() {
- return 'Set (' + this.length + " models)";
}
});
diff --git a/test/collection.js b/test/collection.js
index 58e7030..9a026ac 100644
--- a/test/collection.js
+++ b/test/collection.js
@@ -15,7 +15,7 @@ $(document).ready(function() {
var e = null;
var col = window.col = new Backbone.Collection([a,b,c,d]);
- test("collections: simple and sorted", function() {
+ test("collections: new and sort", function() {
equals(col.first(), a, "a should be first");
equals(col.last(), d, "d should be last");
col.comparator = function(model) { return model.id; };
@@ -25,6 +25,10 @@ $(document).ready(function() {
equals(col.length, 4);
});
+ test("collections: toString", function() {
+ equals(col.toString(), 'Collection (4 models)');
+ });
+
test("collections: get, getByCid", function() {
equals(col.get(1), d);
equals(col.get(3), b);
--
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