[Pkg-javascript-commits] [backbone] 43/173: Fix _onModelEvent regression

Jonas Smedegaard dr at jones.dk
Wed Aug 31 07:44:00 UTC 2016


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

js pushed a commit to branch master
in repository backbone.

commit 67667c5b8fee309ae3dbbbd505b36b0415ea3de8
Author: Justin Ridgewell <justin at ridgewell.name>
Date:   Thu Oct 22 11:06:39 2015 -0400

    Fix _onModelEvent regression
    
    Fixes #3662.
---
 backbone.js        | 18 ++++++++++--------
 test/collection.js | 10 ++++++++++
 2 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/backbone.js b/backbone.js
index bbc63fd..6791904 100644
--- a/backbone.js
+++ b/backbone.js
@@ -1148,14 +1148,16 @@
     // events simply proxy through. "add" and "remove" events that originate
     // in other collections are ignored.
     _onModelEvent: function(event, model, collection, options) {
-      if ((event === 'add' || event === 'remove') && collection !== this) return;
-      if (event === 'destroy') this.remove(model, options);
-      if (event === 'change') {
-        var prevId = this.modelId(model.previousAttributes());
-        var id = this.modelId(model.attributes);
-        if (prevId !== id) {
-          if (prevId != null) delete this._byId[prevId];
-          if (id != null) this._byId[id] = model;
+      if (model) {
+        if ((event === 'add' || event === 'remove') && collection !== this) return;
+        if (event === 'destroy') this.remove(model, options);
+        if (event === 'change') {
+          var prevId = this.modelId(model.previousAttributes());
+          var id = this.modelId(model.attributes);
+          if (prevId !== id) {
+            if (prevId != null) delete this._byId[prevId];
+            if (id != null) this._byId[id] = model;
+          }
         }
       }
       this.trigger.apply(this, arguments);
diff --git a/test/collection.js b/test/collection.js
index 09f8c80..7a6e346 100644
--- a/test/collection.js
+++ b/test/collection.js
@@ -1803,4 +1803,14 @@
     collection.invoke('method', 1, 2, 3);
   });
 
+  QUnit.test('#3662 - triggering change without model will not error', function(assert) {
+    assert.expect(1);
+    var collection = new Backbone.Collection([{id: 1}]);
+    var model = collection.first();
+    collection.on('change', function(model) {
+      assert.equal(model, undefined);
+    });
+    model.trigger('change');
+  });
+
 })();

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