Skip to content
Browse files

Merge pull request #1410 from lukemelia/cp-fix2

Fixed bug in CP setter where observers could be suspended and never restored.
  • Loading branch information...
2 parents 3a8092b + 0c5587a commit 7d6279099e75b0eb887999d52ac7d351aa227bd8 @krisselden krisselden committed
Showing with 36 additions and 26 deletions.
  1. +20 −19 packages/ember-metal/lib/computed.js
  2. +16 −7 packages/ember-metal/tests/computed_test.js
View
39 packages/ember-metal/lib/computed.js
@@ -289,32 +289,33 @@ ComputedPropertyPrototype.set = function(obj, keyName, value) {
hadCachedValue = false,
ret;
this._suspended = obj;
+ try {
+ ret = this.func.call(obj, keyName, value);
- ret = this.func.call(obj, keyName, value);
-
- if (cacheable && keyName in meta.cache) {
- if (meta.cache[keyName] === ret) {
- return;
+ if (cacheable && keyName in meta.cache) {
+ if (meta.cache[keyName] === ret) {
+ return;
+ }
+ hadCachedValue = true;
}
- hadCachedValue = true;
- }
- if (watched) { Ember.propertyWillChange(obj, keyName); }
+ if (watched) { Ember.propertyWillChange(obj, keyName); }
- if (cacheable && hadCachedValue) {
- delete meta.cache[keyName];
- }
-
- if (cacheable) {
- if (!watched && !hadCachedValue) {
- addDependentKeys(this, obj, keyName, meta);
+ if (cacheable && hadCachedValue) {
+ delete meta.cache[keyName];
}
- meta.cache[keyName] = ret;
- }
- if (watched) { Ember.propertyDidChange(obj, keyName); }
+ if (cacheable) {
+ if (!watched && !hadCachedValue) {
+ addDependentKeys(this, obj, keyName, meta);
+ }
+ meta.cache[keyName] = ret;
+ }
- this._suspended = oldSuspended;
+ if (watched) { Ember.propertyDidChange(obj, keyName); }
+ } finally {
+ this._suspended = oldSuspended;
+ }
return ret;
};
View
23 packages/ember-metal/tests/computed_test.js
@@ -576,14 +576,17 @@ testBoth('setting a watched computed property', function(get, set) {
});
testBoth('setting a cached computed property that modifies the value you give it', function(get, set) {
- var obj = {};
+ var obj = {
+ foo: 0
+ };
Ember.defineProperty(obj, 'plusOne', Ember.computed(
function(key, value) {
if (arguments.length > 1) {
+ set(this, 'foo', value);
return value + 1;
}
- return 0;
- }).property().cacheable()
+ return get(this, 'foo') + 1;
+ }).property('foo').cacheable()
);
var plusOneWillChange = 0,
plusOneDidChange = 0;
@@ -594,14 +597,20 @@ testBoth('setting a cached computed property that modifies the value you give it
plusOneDidChange++;
});
- equal(get(obj, 'plusOne'), 0);
- set(obj, 'plusOne', 0);
- equal(get(obj, 'plusOne'), 1);
- set(obj, 'plusOne', 0);
equal(get(obj, 'plusOne'), 1);
+ set(obj, 'plusOne', 1);
+ equal(get(obj, 'plusOne'), 2);
+ set(obj, 'plusOne', 1);
+ equal(get(obj, 'plusOne'), 2);
equal(plusOneWillChange, 1);
equal(plusOneDidChange, 1);
+
+ set(obj, 'foo', 5);
+ equal(get(obj, 'plusOne'), 6);
+
+ equal(plusOneWillChange, 2);
+ equal(plusOneDidChange, 2);
});
module('CP macros');

0 comments on commit 7d62790

Please sign in to comment.
Something went wrong with that request. Please try again.