Skip to content

Commit

Permalink
Merge branch '0-9-stable'
Browse files Browse the repository at this point in the history
Conflicts:
	packages/ember-metal/lib/computed.js
  • Loading branch information
ebryn committed Jun 6, 2012
2 parents 04bb1b0 + fa857ef commit d4e5a4a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 24 deletions.
18 changes: 4 additions & 14 deletions packages/ember-metal/lib/computed.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,8 @@ function mkCpSetter(keyName, desc) {

desc._suspended = this;

watched = watched && m.lastSetValues[keyName] !== guidFor(value);
if (watched) {
m.lastSetValues[keyName] = guidFor(value);
Ember.propertyWillChange(this, keyName);
}

if (cacheable) { delete m.cache[keyName]; }
if (watched) { Ember.propertyWillChange(this, keyName); }
if (cacheable) delete m.cache[keyName];
ret = func.call(this, keyName, value);
if (cacheable) { m.cache[keyName] = ret; }
if (watched) { Ember.propertyDidChange(this, keyName); }
Expand Down Expand Up @@ -340,13 +335,8 @@ ComputedPropertyPrototype.set = function(obj, keyName, value) {

this._suspended = obj;

watched = watched && m.lastSetValues[keyName] !== guidFor(value);
if (watched) {
m.lastSetValues[keyName] = guidFor(value);
Ember.propertyWillChange(obj, keyName);
}

if (cacheable) { delete m.cache[keyName]; }
if (watched) { Ember.propertyWillChange(obj, keyName); }
if (cacheable) delete m.cache[keyName];
ret = this.func.call(obj, keyName, value);
if (cacheable) { m.cache[keyName] = ret; }
if (watched) { Ember.propertyDidChange(obj, keyName); }
Expand Down
27 changes: 17 additions & 10 deletions packages/ember-metal/tests/observer_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -621,24 +621,31 @@ testBoth('setting simple prop should not trigger', function(get, set) {
equal(count, 1, 'should not trigger observer again');
});

testBoth('setting computed prop with same value should not trigger', function(get, set) {

// The issue here is when a computed property is directly set with a value, then has a
// dependent key change (which triggers a cache expiration and recomputation), observers will
// not be fired if the CP setter is called with the last set value.
testBoth('setting a cached computed property whose value has changed should trigger', function(get, set) {
var obj = {};

Ember.defineProperty(obj, 'foo', Ember.computed(function(key, value) {
if (value !== undefined) this._value = value+' X';
return this._value;
}));
if (arguments.length === 2) { return value; }
return get(this, 'baz');
}).property('baz').cacheable());

var count = 0;

Ember.addObserver(obj, 'foo', function() { count++; });

set(obj, 'foo', 'bar');
equal(count, 1, 'should trigger observer since we do not have existing val');
equal(count, 1);
equal(get(obj, 'foo'), 'bar');

set(obj, 'foo', 'baz');
equal(count, 2, 'should trigger observer');
set(obj, 'baz', 'qux');
equal(count, 2);
equal(get(obj, 'foo'), 'qux');

set(obj, 'foo', 'baz');
equal(count, 2, 'should not trigger observer again');
get(obj, 'foo');
set(obj, 'foo', 'bar');
equal(count, 3);
equal(get(obj, 'foo'), 'bar');
});

0 comments on commit d4e5a4a

Please sign in to comment.