[Pkg-javascript-commits] [backbone] 27/34: Merging in Collection#toJSON
Jonas Smedegaard
js at moszumanska.debian.org
Sat May 3 16:58:54 UTC 2014
This is an automated email from the git hooks/post-receive script.
js pushed a commit to tag 0.2.0
in repository backbone.
commit f3a8dcd16b186607458900883e9753487e5e2df1
Author: Jeremy Ashkenas <jashkenas at gmail.com>
Date: Wed Oct 20 16:01:56 2010 -0400
Merging in Collection#toJSON
---
backbone.js | 7 ++++---
index.html | 21 +++++++++++++++++++++
test/collection.js | 4 ++++
3 files changed, 29 insertions(+), 3 deletions(-)
diff --git a/backbone.js b/backbone.js
index c757a5c..cc84a9b 100644
--- a/backbone.js
+++ b/backbone.js
@@ -329,10 +329,11 @@
// The default model for a collection is just a **Backbone.Model**.
// This should be overridden in most cases.
model : Backbone.Model,
-
- // Return a array containing each model's toJSON result.
+
+ // The JSON representation of a Collection is an array of the
+ // models' attributes.
toJSON : function() {
- return this.models.map(function(model){ return model.toJSON(); })
+ return this.map(function(model){ return model.toJSON(); });
},
// Add a model, or list of models to the set. Pass **silent** to avoid
diff --git a/index.html b/index.html
index bc833ba..8bf965a 100644
--- a/index.html
+++ b/index.html
@@ -180,6 +180,7 @@
<li>– <a href="#Collection-model">model</a></li>
<li>– <a href="#Collection-constructor">constructor / initialize</a></li>
<li>– <a href="#Collection-models">models</a></li>
+ <li>– <a href="#Collection-toJSON">toJSON</a></li>
<li>– <a href="#Collection-Underscore-Methods"><b>Underscore Methods (25)</b></a></li>
<li>– <a href="#Collection-add">add</a></li>
<li>– <a href="#Collection-remove">remove</a></li>
@@ -849,6 +850,26 @@ var tabs = new TabSet([tab1, tab2, tab3]);
to access model objects, but occasionally a direct reference to the array
is desired.
</p>
+
+ <p id="Collection-toJSON">
+ <b class="header">toJSON</b><code>collection.toJSON()</code>
+ <br />
+ Return an array containing the attributes hash of each model in the
+ collection. This can be used to serialize and persist the
+ collection as a whole. The name of this method is a bit confusing, because
+ it conforms to
+ <a href="https://developer.mozilla.org/en/JSON#toJSON()_method">JavaScript's JSON API</a>.
+ </p>
+
+<pre class="runnable">
+var collection = new Backbone.Collection([
+ {name: "Tim", age: 5},
+ {name: "Ida", age: 26},
+ {name: "Rob", age: 55}
+]);
+
+alert(JSON.stringify(collection));
+</pre>
<p id="Collection-Underscore-Methods">
<b class="header">Underscore Methods (25)</b>
diff --git a/test/collection.js b/test/collection.js
index 17cacee..46dd0f5 100644
--- a/test/collection.js
+++ b/test/collection.js
@@ -82,6 +82,10 @@ $(document).ready(function() {
equals(coll.one, 1);
});
+ test("Collection: toJSON", function() {
+ equals(JSON.stringify(col), '[{"id":1,"label":"d"},{"id":2,"label":"c"},{"id":3,"label":"b"},{"id":4,"label":"a"}]');
+ });
+
test("Collection: Underscore methods", function() {
equals(col.map(function(model){ return model.get('label'); }).join(' '), 'd c b a');
equals(col.any(function(model){ return model.id === 100; }), false);
--
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