Skip to content

Commit

Permalink
Merge pull request #541 from omghax/master
Browse files Browse the repository at this point in the history
Resolved issue with removeObserver and @each
  • Loading branch information
Colin Campbell committed Sep 27, 2011
2 parents 8236876 + 101513f commit 328be9c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
9 changes: 7 additions & 2 deletions frameworks/runtime/private/chain_observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,13 @@ SC._ChainObserver.prototype = {

// remove observer
var obj = this.object ;
if (obj && obj.removeObserver) {
obj.removeObserver(this.property, this, this.propertyDidChange) ;
if (obj) {
if (this.property === '@each' && this.next && obj._removeContentObserver) {
obj._removeContentObserver(this);
}
if (obj.removeObserver) {
obj.removeObserver(this.property, this, this.propertyDidChange) ;
}
}

// destroy next item in chain
Expand Down
20 changes: 20 additions & 0 deletions frameworks/runtime/tests/mixins/observable/chained.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,23 @@ test("chained observers on enumerable properties are triggered when the observed
equals(observerFiredCount, 0, "observer did not fire after removing changing property on a removed object");
});

test("content observers are removed correctly", function() {
var child1 = SC.Object.create({ name: "Bartholomew", age: 15 });
var child2 = SC.Object.create({ name: "Agnes", age: 12 });
var children = [child1, child2];

var observerFiredCount = 0;
var observerFunc = function() { observerFiredCount++; }

children.addObserver('@each.name', this, observerFunc);
children.removeObserver('@each.name', this, observerFunc);
observerFiredCount = 0;
SC.run(function() { children.setEach('name', "Hanna"); });
equals(observerFiredCount, 0, "name observer did not fire after it was removed");

children.addObserver('@each.age', this, observerFunc);
children.removeObserver('@each.age', this, observerFunc);
observerFiredCount = 0;
SC.run(function() { children.setEach('age', 14); });
equals(observerFiredCount, 0, "age observer did not fire after it was removed");
});

0 comments on commit 328be9c

Please sign in to comment.