[FIX] unbind handlers on records from a collection when the record is removed from the collection or the collection is reset

bzr revid: xmo@openerp.com-20120112170917-n6xadpzkbflk9s2c
This commit is contained in:
Xavier Morel 2012-01-12 18:09:17 +01:00
parent 378c20491b
commit 8f4a0277ac
3 changed files with 23 additions and 1 deletions

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

View 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 = [];

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