Skip to content

Commit

Permalink
Merge pull request #456 from canjs/unregister-list-events
Browse files Browse the repository at this point in the history
[3.x] Use the single reference for turning off both index and key listeners
  • Loading branch information
bmomberger-bitovi authored May 24, 2019
2 parents e963a7c + ae4570c commit f0e51d0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
29 changes: 29 additions & 0 deletions list/list-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1287,3 +1287,32 @@ QUnit.test("Bound serialized lists update when they change length", function(){
list.push("toast");
canReflect.offValue(obs, onChange);
});


QUnit.test('listening to index and key events with symbol funciton', function() {
QUnit.expect(2);
var list = new DefineList([ "a", "b" ]);
var indexListenerOk = true;
var keyListenerOk = true;

var indexHandler, keyHandler;

list[canSymbol.for("can.onKeyValue")](0, indexHandler = function() {
QUnit.ok(indexListenerOk, 'index callback (once only).');
indexListenerOk = false;
});

list[canSymbol.for("can.onKeyValue")]('foo', keyHandler = function() {
QUnit.ok(keyListenerOk, 'key callback (once only).');
keyListenerOk = false;
});

list.splice(0, 2, "c", "d");
list.set("foo", "bar");

list[canSymbol.for("can.offKeyValue")](0, indexHandler);
list[canSymbol.for("can.offKeyValue")]('foo', keyHandler);

list.splice(0, 2, "e", "f");
list.set("foo", "baz");
});
6 changes: 1 addition & 5 deletions list/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -1533,15 +1533,11 @@ canReflect.assignSymbols(DefineList.prototype,{
},
// Called when a property reference is removed
"can.offKeyValue": function(key, handler) {
var translationHandler;
var translationHandler = singleReference.getAndDelete(handler, this, key);
if (isNaN(key)) {
translationHandler = function(ev, newValue, oldValue) {
handler(newValue, oldValue);
};
this.removeEventListener(key, translationHandler);
}
else {
translationHandler = singleReference.getAndDelete(handler, this, key);
this.removeEventListener('length', translationHandler);
}
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@
"steal": "^1.0.7",
"steal-qunit": "^1.0.0",
"steal-tools": "^1.4.0",
"testee": "^0.7.0"
"testee": "^0.9.1"
}
}

0 comments on commit f0e51d0

Please sign in to comment.