[Pkg-javascript-commits] [backbone] 115/281: Col.fetch() should create models with parse:true:

Jonas Smedegaard js at moszumanska.debian.org
Sat May 3 17:02:02 UTC 2014


This is an automated email from the git hooks/post-receive script.

js pushed a commit to tag 0.9.0
in repository backbone.

commit 67f689df9c460c10c61d7a8722f303eb61d4b642
Author: Magnus Holm <judofyr at gmail.com>
Date:   Sun Dec 4 18:13:00 2011 +0100

    Col.fetch() should create models with parse:true:
    
    This is done in two steps:
    
    1. Col.fetch() now defaults to parse:true
    2. Col.reset() and _prepareModel now passes along
       parse:true.
    
    This means that Col.add() also accepts the
    parse:true option.
---
 backbone.js        |  5 +++--
 test/collection.js | 18 ++++++++++++++++++
 2 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/backbone.js b/backbone.js
index 805ce42..ea449c5 100644
--- a/backbone.js
+++ b/backbone.js
@@ -475,7 +475,7 @@
       options || (options = {});
       this.each(this._removeReference);
       this._reset();
-      this.add(models, {silent: true});
+      this.add(models, {silent: true, parse: options.parse});
       if (!options.silent) this.trigger('reset', this, options);
       return this;
     },
@@ -485,6 +485,7 @@
     // models to the collection instead of resetting.
     fetch : function(options) {
       options || (options = {});
+      if (options.parse === undefined) options.parse = true;
       var collection = this;
       var success = options.success;
       options.success = function(resp, status, xhr) {
@@ -537,7 +538,7 @@
     _prepareModel : function(model, options) {
       if (!(model instanceof Backbone.Model)) {
         var attrs = model;
-        model = new this.model(attrs, {collection: this});
+        model = new this.model(attrs, {collection: this, parse: options.parse});
         if (model.validate && !model._performValidation(model.attributes, options)) model = false;
       } else if (!model.collection) {
         model.collection = this;
diff --git a/test/collection.js b/test/collection.js
index 80beeb9..005b044 100644
--- a/test/collection.js
+++ b/test/collection.js
@@ -179,6 +179,20 @@ $(document).ready(function() {
     equals(e.collection, colE);
   });
 
+  test("Collection: add model with parse", function() {
+    var Model = Backbone.Model.extend({
+      parse: function(obj) {
+        obj.value += 1;
+        return obj;
+      }
+    });
+
+    var Col = Backbone.Collection.extend({model: Model});
+    var col = new Col;
+    col.add({value: 1}, {parse: true});
+    equals(col.at(0).get('value'), 2);
+  });
+
   test("Collection: remove", function() {
     var removed = otherRemoved = null;
     col.bind('remove', function(model){ removed = model.get('label'); });
@@ -304,6 +318,10 @@ $(document).ready(function() {
     col.fetch();
     equals(lastRequest[0], 'read');
     equals(lastRequest[1], col);
+    equals(lastRequest[2].parse, true);
+
+    col.fetch({parse: false});
+    equals(lastRequest[2].parse, false);
   });
 
   test("Collection: create", function() {

-- 
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