Skip to content

Commit

Permalink
[BUGFIX beta] Add warning for “deep @each” usage in dependent keys
Browse files Browse the repository at this point in the history
  • Loading branch information
jgwhite committed Jan 21, 2016
1 parent 48562e6 commit 4cd4615
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"expectAssertion",
"expectDeprecation",
"expectNoDeprecation",
"expectWarning",
"expectNoWarning",
"ignoreAssertion",
"ignoreDeprecation",

Expand Down
11 changes: 10 additions & 1 deletion packages/ember-metal/lib/computed.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert } from 'ember-metal/debug';
import { assert, warn } from 'ember-metal/debug';
import { set } from 'ember-metal/property_set';
import { inspect } from 'ember-metal/utils';
import { meta as metaFor, peekMeta } from 'ember-metal/meta';
Expand All @@ -25,6 +25,8 @@ import {

function UNDEFINED() { }

const DEEP_EACH_REGEX = /\.@each\.[^.]+\./;

// ..........................................................
// COMPUTED PROPERTY
//
Expand Down Expand Up @@ -252,6 +254,13 @@ ComputedPropertyPrototype.property = function() {
var args;

var addArg = function(property) {
warn(
`Dependent keys containing @each only work one level deep. ` +
`You cannot use nested forms like todos.@each.owner.name or todos.@each.owner.@each.name. ` +
`Please create an intermediary computed property.`,
DEEP_EACH_REGEX.test(property) === false,
{ id: 'ember-metal.computed-deep-each' }
);
args.push(property);
};

Expand Down
22 changes: 22 additions & 0 deletions packages/ember-metal/tests/computed_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,28 @@ QUnit.test('defining a computed property with a dependent key ending with @each
deepEqual(cp._dependentKeys, ['qux', 'zoopa.[]']);
});

QUnit.test('defining a computed property with a dependent key more than one level deep beyond @each is not supported', function() {
let warning = `Dependent keys containing @each only work one level deep. ` +
`You cannot use nested forms like todos.@each.owner.name or todos.@each.owner.@each.name. ` +
`Please create an intermediary computed property.`;

expectNoWarning(() => {
computed('todos', () => {});
});

expectNoWarning(() => {
computed('todos.@each.owner', () => {});
});

expectWarning(() => {
computed('todos.@each.owner.name', () => {});
}, warning);

expectWarning(() => {
computed('todos.@each.owner.@each.name', () => {});
}, warning);
});

var objA, objB;
QUnit.module('computed should inherit through prototype', {
setup() {
Expand Down

0 comments on commit 4cd4615

Please sign in to comment.