[Pkg-javascript-commits] [backbone] 220/281: Fixes #563. Initial draft of 'wait: true' for pessimistic instead of optimistic save() create() and destroy(). Makes it easier to have your asynchronous UI and eat it too.

Jonas Smedegaard js at moszumanska.debian.org
Sat May 3 17:02:15 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 cea56e523a3042e280ae81118657542c6b85c8e9
Author: Jeremy Ashkenas <jashkenas at gmail.com>
Date:   Tue Jan 17 15:26:26 2012 -0500

    Fixes #563. Initial draft of 'wait: true' for pessimistic instead of optimistic save() create() and destroy(). Makes it easier to have your asynchronous UI and eat it too.
---
 backbone.js        | 31 +++++++++++++++++++++----------
 test/collection.js |  2 +-
 test/model.js      |  3 ++-
 test/sync.js       |  4 ++--
 4 files changed, 26 insertions(+), 14 deletions(-)

diff --git a/backbone.js b/backbone.js
index cc520aa..6009e63 100644
--- a/backbone.js
+++ b/backbone.js
@@ -302,11 +302,13 @@
       }
 
       options = options ? _.clone(options) : {};
-      if (attrs && !this.set(attrs, options)) return false;
+      if (attrs && !this[options.wait ? '_performValidation' : 'set'](attrs, options)) return false;
       var model = this;
       var success = options.success;
       options.success = function(resp, status, xhr) {
-        if (!model.set(model.parse(resp, xhr), options)) return false;
+        var serverAttrs = model.parse(resp, xhr);
+        if (options.wait) serverAttrs = _.extend(attrs || {}, serverAttrs);
+        if (!model.set(serverAttrs, options)) return false;
         if (success) {
           success(model, resp);
         } else {
@@ -319,14 +321,20 @@
     },
 
     // Destroy this model on the server if it was already persisted.
-    // Upon success, the model is removed from its collection, if it has one.
+    // Optimistically removes the model from its collection, if it has one.
+    // If `wait: true` is passed, waits for the server to respond before removal.
     destroy : function(options) {
       options = options ? _.clone(options) : {};
-      if (this.isNew()) return this.trigger('destroy', this, this.collection, options);
       var model = this;
       var success = options.success;
-      options.success = function(resp) {
+
+      var triggerDestroy = function() {
         model.trigger('destroy', model, model.collection, options);
+      };
+
+      if (this.isNew()) return triggerDestroy();
+      options.success = function(resp) {
+        if (options.wait) triggerDestroy();
         if (success) {
           success(model, resp);
         } else {
@@ -334,7 +342,9 @@
         }
       };
       options.error = Backbone.wrapError(options.error, model, options);
-      return (this.sync || Backbone.sync).call(this, 'delete', this, options);
+      var xhr = (this.sync || Backbone.sync).call(this, 'delete', this, options);
+      if (!options.wait) triggerDestroy();
+      return xhr;
     },
 
     // Default URL for the model's representation on the server -- if you're
@@ -580,17 +590,18 @@
       return (this.sync || Backbone.sync).call(this, 'read', this, options);
     },
 
-    // Create a new instance of a model in this collection. After the model
-    // has been created on the server, it will be added to the collection.
-    // Returns the model, or 'false' if validation on a new model fails.
+    // Create a new instance of a model in this collection. Add the model to the
+    // collection immediately, unless `wait: true` is passed, in which case we
+    // wait for the server to agree.
     create : function(model, options) {
       var coll = this;
       options = options ? _.clone(options) : {};
       model = this._prepareModel(model, options);
       if (!model) return false;
+      if (!options.wait) coll.add(model, options);
       var success = options.success;
       options.success = function(nextModel, resp, xhr) {
-        coll.add(nextModel, options);
+        if (options.wait) coll.add(nextModel, options);
         if (success) {
           success(nextModel, resp);
         } else {
diff --git a/test/collection.js b/test/collection.js
index c093645..b836486 100644
--- a/test/collection.js
+++ b/test/collection.js
@@ -346,7 +346,7 @@ $(document).ready(function() {
   });
 
   test("Collection: create", function() {
-    var model = col.create({label: 'f'});
+    var model = col.create({label: 'f'}, {wait: true});
     equals(lastRequest[0], 'create');
     equals(lastRequest[1], model);
     equals(model.get('label'), 'f');
diff --git a/test/model.js b/test/model.js
index 1980668..cbf7272 100644
--- a/test/model.js
+++ b/test/model.js
@@ -356,7 +356,8 @@ $(document).ready(function() {
     attrs = { 'foo': 1, 'bar': 2, 'baz': 3};
     a = new Backbone.Model(attrs);
     a.sync = function() { throw "should not be called"; };
-    ok(a.destroy(), "non-persisted model should not call sync");
+    a.destroy();
+    ok(true, "non-persisted model should not call sync");
   });
 
   test("Model: validate", function() {
diff --git a/test/sync.js b/test/sync.js
index b219f6e..c92cb3a 100644
--- a/test/sync.js
+++ b/test/sync.js
@@ -36,7 +36,7 @@ $(document).ready(function() {
   });
 
   test("sync: create", function() {
-    library.add(library.create(attrs));
+    library.create(attrs, {wait: false});
     equals(lastRequest.url, '/library');
     equals(lastRequest.type, 'POST');
     equals(lastRequest.dataType, 'json');
@@ -106,7 +106,7 @@ $(document).ready(function() {
   });
 
   test("sync: destroy", function() {
-    library.first().destroy();
+    library.first().destroy({wait: true});
     equals(lastRequest.url, '/library/2-the-tempest');
     equals(lastRequest.type, 'DELETE');
     equals(lastRequest.data, null);

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