[Pkg-javascript-commits] [backbone] 114/281: parse:true runs the attributes through parse():

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 bdbcfa9da28e86313491de515709625c569e2bb6
Author: Magnus Holm <judofyr at gmail.com>
Date:   Sun Dec 4 17:48:47 2011 +0100

    parse:true runs the attributes through parse():
    
    `new Model(attr, {parse:true})` will now call
    Model.prototype.parse(attr). This is useful if
    you want to create a model out of an object
    structure from an external server.
---
 backbone.js   |  1 +
 test/model.js | 11 +++++++++++
 2 files changed, 12 insertions(+)

diff --git a/backbone.js b/backbone.js
index dabed49..805ce42 100644
--- a/backbone.js
+++ b/backbone.js
@@ -127,6 +127,7 @@
   Backbone.Model = function(attributes, options) {
     var defaults;
     attributes || (attributes = {});
+    if (options && options.parse) attributes = this.parse(attributes);
     if (defaults = this.defaults) {
       if (_.isFunction(defaults)) defaults = defaults.call(this);
       attributes = _.extend({}, defaults, attributes);
diff --git a/test/model.js b/test/model.js
index b0e0596..85c6602 100644
--- a/test/model.js
+++ b/test/model.js
@@ -51,6 +51,17 @@ $(document).ready(function() {
     equals(model.one, 1);
   });
 
+  test("Model: initialize with parsed attributes", function() {
+    var Model = Backbone.Model.extend({
+      parse: function(obj) {
+        obj.value += 1;
+        return obj;
+      }
+    });
+    var model = new Model({value: 1}, {parse: true});
+    equals(model.get('value'), 2);
+  });
+
   test("Model: url", function() {
     equals(doc.url(), '/collection/1-the-tempest');
     doc.collection.url = '/collection/';

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