Skip to content

Commit 4d44581

Browse files
author
Justin Lan
committed
Add a message explaining how to fix computed properties with no dependent keys.
Also updated the guide.
1 parent f3a6f3b commit 4d44581

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
# Require Dependent Keys
22

3-
One can think of computed properties as cached accessors, that broadcast change events if consumed, and their dependent key has changed. In-general computed properties that do not mutate state, will just do the right thing. They will be on-demand, and data will flow nicely.
3+
You can think of computed properties as cached accessors.
4+
They broadcast change events if consumed and their dependent keys have changed.
45

5-
```
6+
```javascript
67
export default Ember.Component.extend({
7-
fullName: Ember.computed('first', 'last', function() {
8-
return this.get('first') + ' ' + this.get('last');
8+
fullName: Ember.computed('firstName', 'lastName', function() {
9+
return this.get('firstName') + ' ' + this.get('lastName');
910
});
1011
});
1112
```
1213

13-
However, for computed properties to work correctly they must enumerate their dependencies. If they do not enumerate their dependencies you run the risk of the CP cache being stale or eagerly computed. This rule makes sure that when you use a computed property it has dependent keys.
14+
However, for computed properties to work correctly you must enumerate the properties they depend on.
15+
If you do not, you run the risk of the Computed Property cache being stale or eagerly computed.
16+
This rule makes sure that when you use a computed property you list its dependent keys.
17+
18+
A computed property without dependent keys is only initilized once. If that is the desired behavior, you should instead initialize the property in `init()`.

lib/rules/require-dependent-keys.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
const { get } = require('../utils/get');
7-
const MESSAGE = 'Do not use Computed Properties without dependent keys. Please see following guide for more information: https://github.com/chadhietala/ember-best-practices/blob/master/guides/rules/require-dependent-keys.md';
7+
const MESSAGE = 'Do not use Computed Properties without dependent keys. If you want the property to be set only once, initialize it in init(). Please see following guide for more information: https://github.com/chadhietala/ember-best-practices/blob/master/guides/rules/require-dependent-keys.md';
88

99
function isMissingDependentKeys(name, node, context) {
1010
if (name === 'computed' && node.arguments.length === 1) {

0 commit comments

Comments
 (0)