[FIX] unbind handlers on records from a collection when the record is removed from...
authorXavier Morel <xmo@openerp.com>
Thu, 12 Jan 2012 17:09:17 +0000 (18:09 +0100)
committerXavier Morel <xmo@openerp.com>
Thu, 12 Jan 2012 17:09:17 +0000 (18:09 +0100)
bzr revid: xmo@openerp.com-20120112170917-n6xadpzkbflk9s2c

addons/web/static/src/js/view_list.js
addons/web/static/test/list-utils.js
addons/web/static/test/test.html

index 4e6adbf..aa970c3 100644 (file)
@@ -1694,6 +1694,7 @@ var Collection = openerp.web.Class.extend(/** @lends Collection# */{
             proxy.reset();
         });
         this._proxies = {};
+        _(this.records).invoke('unbind', null, this._onRecordEvent);
         this.length = 0;
         this.records = [];
         this._byId = {};
@@ -1712,7 +1713,6 @@ var Collection = openerp.web.Class.extend(/** @lends Collection# */{
      * @returns this
      */
     remove: function (record) {
-        var self = this;
         var index = _(this.records).indexOf(record);
         if (index === -1) {
             _(this._proxies).each(function (proxy) {
@@ -1721,6 +1721,7 @@ var Collection = openerp.web.Class.extend(/** @lends Collection# */{
             return this;
         }
 
+        record.unbind(null, this._onRecordEvent);
         this.records.splice(index, 1);
         delete this._byId[record.get('id')];
         this.length--;
index f71da82..8cc3445 100644 (file)
@@ -170,6 +170,16 @@ $(document).ready(function () {
         equal(c.get(2), undefined);
         strictEqual(c.at(1).get('value'), 20);
     });
+    test('Remove unbind', function () {
+        var changed = false,
+            c = new openerp.web.list.Collection([ {id: 1, value: 5} ]);
+        c.bind('change', function () { changed = true; });
+        var record = c.get(1);
+        c.remove(record);
+        record.set('value', 42);
+        ok(!changed, 'removed records should not trigger events in their ' +
+                     'parent collection');
+    });
     test('Reset', function () {
         var event, obj, c = new openerp.web.list.Collection([
             {id: 1, value: 5},
@@ -190,6 +200,16 @@ $(document).ready(function () {
         strictEqual(c.length, 1);
         strictEqual(c.get(42).get('value'), 55);
     });
+    test('Reset unbind', function () {
+        var changed = false,
+            c = new openerp.web.list.Collection([ {id: 1, value: 5} ]);
+        c.bind('change', function () { changed = true; });
+        var record = c.get(1);
+        c.reset();
+        record.set('value', 42);
+        ok(!changed, 'removed records should not trigger events in their ' +
+                     'parent collection');
+    });
 
     test('Events propagation', function () {
         var values = [];
index b1018bc..da06aba 100644 (file)
@@ -14,6 +14,7 @@
     <!-- jquery -->
     <script src="/web/static/lib/jquery/jquery-1.6.4.js"></script>
     <script src="/web/static/lib/jquery.ui/js/jquery-ui-1.8.9.custom.min.js"></script>
+    <script src="/web/static/lib/jquery.ba-bbq/jquery.ba-bbq.js"></script>
     
     <script src="/web/static/lib/datejs/globalization/en-US.js"></script>
     <script src="/web/static/lib/datejs/core.js"></script>