Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add deprecation for old usages of cachable and readOnly in CPs #9489

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions packages/ember-metal/lib/computed.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,11 @@ function ComputedProperty(func, opts) {
this._suspended = undefined;
this._meta = undefined;

this._cacheable = (opts && opts.cacheable !== undefined) ? opts.cacheable : true;
Ember.deprecate("Passing opts.cacheable to the CP constructor is deprecated. Invoke `volatile()` on the CP instead.", !opts || !opts.hasOwnProperty('cacheable'));
this._cacheable = (opts && opts.cacheable !== undefined) ? opts.cacheable : true; // TODO: Set always to `true` once this deprecation is gone.
this._dependentKeys = opts && opts.dependentKeys;
this._readOnly = opts && (opts.readOnly !== undefined || !!opts.readOnly) || false;
Ember.deprecate("Passing opts.readOnly to the CP constructor is deprecated. All CPs are writable by default. Yo can invoke `readOnly()` on the CP to change this.", !opts || !opts.hasOwnProperty('readOnly'));
this._readOnly = opts && (opts.readOnly !== undefined || !!opts.readOnly) || false; // TODO: Set always to `false` once this deprecation is gone.
}

ComputedProperty.prototype = new Descriptor();
Expand All @@ -143,8 +145,10 @@ var ComputedPropertyPrototype = ComputedProperty.prototype;
@param {Boolean} aFlag optional set to `false` to disable caching
@return {Ember.ComputedProperty} this
@chainable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to be marked as @deprecated also.

@deprecated All computed properties are cacheble by default. Use `volatile()` instead to opt-out to caching.
*/
ComputedPropertyPrototype.cacheable = function(aFlag) {
Ember.deprecate('ComputedProperty.cacheable() is deprecated. All computed properties are cacheable by default.');
this._cacheable = aFlag !== false;
return this;
};
Expand All @@ -166,7 +170,8 @@ ComputedPropertyPrototype.cacheable = function(aFlag) {
@chainable
*/
ComputedPropertyPrototype.volatile = function() {
return this.cacheable(false);
this._cacheable = false;
return this;
};

/**
Expand All @@ -190,7 +195,8 @@ ComputedPropertyPrototype.volatile = function() {
@chainable
*/
ComputedPropertyPrototype.readOnly = function(readOnly) {
this._readOnly = readOnly === undefined || !!readOnly;
Ember.deprecate('Passing arguments to ComputedProperty.readOnly() is deprecated.', arguments.length === 0);
this._readOnly = readOnly === undefined || !!readOnly; // Force to true once this deprecation is gone
return this;
};

Expand Down Expand Up @@ -502,7 +508,7 @@ ComputedPropertyPrototype.teardown = function(obj, keyName) {
The function should accept two parameters, key and value. If value is not
undefined you should set the value first. In either case return the
current value of the property.

A computed property defined in this way might look like this:

```js
Expand All @@ -518,7 +524,7 @@ ComputedPropertyPrototype.teardown = function(obj, keyName) {
var client = Person.create();

client.get('fullName'); // 'Betty Jones'

client.set('lastName', 'Fuller');
client.get('fullName'); // 'Betty Fuller'
```
Expand Down
4 changes: 3 additions & 1 deletion packages/ember-metal/lib/injected_property.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ function InjectedProperty(type, name) {
"instantiated via a container.", this.container);

return this.container.lookup(type + ':' + (name || keyName));
}, { readOnly: true });
});
this.readOnly();
}

InjectedProperty.prototype = create(Descriptor.prototype);
Expand All @@ -37,6 +38,7 @@ var ComputedPropertyPrototype = ComputedProperty.prototype;
InjectedPropertyPrototype._super$Constructor = ComputedProperty;

InjectedPropertyPrototype.get = ComputedPropertyPrototype.get;
InjectedPropertyPrototype.readOnly = ComputedPropertyPrototype.readOnly;

InjectedPropertyPrototype.set = function(obj, keyName) {
throw new EmberError("Cannot set injected property '" + keyName + "' on object: " + inspect(obj));
Expand Down
26 changes: 26 additions & 0 deletions packages/ember-metal/tests/computed_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,32 @@ testBoth('modifying a cacheable property should update cache', function(get, set
equal(count, 2, 'should not invoke again');
});

test('calling cacheable() on a computed property raises a deprecation', function(){
var cp = new ComputedProperty(function(){});
expectDeprecation(function(){
cp.cacheable();
}, 'ComputedProperty.cacheable() is deprecated. All computed properties are cacheable by default.');
});

test('passing cacheable in a the options to the CP constructor raises a deprecation', function(){
expectDeprecation(function(){
new ComputedProperty(function(){}, { cacheable: true });
}, "Passing opts.cacheable to the CP constructor is deprecated. Invoke `volatile()` on the CP instead.");
});

test('calling readOnly() on a computed property with arguments raises a deprecation', function(){
var cp = new ComputedProperty(function(){});
expectDeprecation(function(){
cp.readOnly(true);
}, 'Passing arguments to ComputedProperty.readOnly() is deprecated.');
});

test('passing readOnly in a the options to the CP constructor raises a deprecation', function(){
expectDeprecation(function(){
new ComputedProperty(function(){}, { readOnly: false });
}, "Passing opts.readOnly to the CP constructor is deprecated. All CPs are writable by default. Yo can invoke `readOnly()` on the CP to change this.");
});

testBoth('inherited property should not pick up cache', function(get, set) {
var objB = create(obj);

Expand Down
2 changes: 1 addition & 1 deletion packages/ember-runtime/lib/computed/reduce_computed.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,13 +476,13 @@ function ReduceComputedProperty(options) {

this.options = options;
this._dependentKeys = null;
this._cacheable = true;
// A map of dependentKey -> [itemProperty, ...] that tracks what properties of
// items in the array we must track to update this property.
this._itemPropertyKeys = {};
this._previousItemPropertyKeys = {};

this.readOnly();
this.cacheable();

this.recomputeOnce = function(propertyName) {
// What we really want to do is coalesce by <cp, propertyName>.
Expand Down