Skip to content

Commit

Permalink
Improve compatibility with Ember.ArrayProxy. Fixes #109 (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwpastore authored and Kelly Selden committed Jul 7, 2017
1 parent eba3ef8 commit 8a9f33a
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 12 deletions.
14 changes: 9 additions & 5 deletions addon/collapse-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ export default function(property) {
return [property];
}

let atEachIndex = property.indexOf('.@each');
if (atEachIndex !== -1) {
return [property.slice(0, atEachIndex)];
} else if (property.slice(-2) === '[]') {
return [property.slice(0, -3)];
let atEachIndex = property.indexOf('@each.');
if (atEachIndex === -1) {
atEachIndex = property.indexOf('[]');
}

if (atEachIndex === 0) {
return [''];
} else if (atEachIndex > 0) {
return [property.slice(0, atEachIndex - 1)];
}

return expandProperty(property);
Expand Down
2 changes: 1 addition & 1 deletion addon/create-class-computed.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export default function(observerBools, macroGenerator) {
function getOriginalArrayDecorator(key, i) {
if (typeof key === 'string') {
let originalKey = keys[keyMap[i]];
if (originalKey.indexOf('.[]') !== -1 || originalKey.indexOf('.@each') !== -1) {
if (originalKey.indexOf('[]') !== -1 || originalKey.indexOf('@each.') !== -1) {
return originalKey;
}
}
Expand Down
7 changes: 7 additions & 0 deletions addon/get-value.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { get } from '@ember/object';
import { isBlank } from '@ember/utils';
import isComputed from './is-computed';

export default function({ context, macro, key } = {}) {
Expand All @@ -10,5 +11,11 @@ export default function({ context, macro, key } = {}) {
return macro;
}

if (isBlank(macro)) {
// the macro was `[]' or `@each.key', which has been trimmed, leaving a
// blank string, so return the context (which is likely an ArrayProxy)
return context;
}

return get(context, macro);
}
14 changes: 9 additions & 5 deletions addon/normalize-array-key.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { isBlank } from '@ember/utils';

export default function(array, keys = []) {
// this macro support should be extracted out
// we should only deal with string keys in here
Expand All @@ -7,7 +9,7 @@ export default function(array, keys = []) {

let props;

let i = array.indexOf('.@each');
let i = array.indexOf('@each.');
if (i !== -1) {
let chain = array.split('.');
let end = chain[chain.length - 1];
Expand All @@ -17,12 +19,14 @@ export default function(array, keys = []) {
props = [end];
}
} else {
i = array.indexOf('.[]');
i = array.indexOf('[]');
props = [];
}

if (i !== -1) {
array = array.substr(0, i);
if (i === 0) {
array = ''
} else if (i > 0) {
array = array.slice(0, i - 1);
}

keys.forEach(key => {
Expand All @@ -48,5 +52,5 @@ export default function(array, keys = []) {
}
}

return `${array}.${suffix}`;
return isBlank(array) ? suffix : `${array}.${suffix}`;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"ember-cli-shims": "^1.1.0",
"ember-data": "^2.13.2",
"ember-disable-prototype-extensions": "^1.1.2",
"ember-macro-test-helpers": "^3.0.0",
"ember-macro-test-helpers": "^3.1.0",
"ember-resolver": "^4.0.0",
"ember-sinon": "0.7.0",
"ember-source": "~2.14.0",
Expand Down
26 changes: 26 additions & 0 deletions tests/integration/computed-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { module } from 'qunit';
import sinon from 'sinon';
import compute from 'ember-macro-test-helpers/compute';
import namedTest from '../helpers/named-test';
import ArrayProxy from '@ember/array/proxy';
import { A as emberA } from '@ember/array';

const getReturnValue = 'get return value test';
const setReturnValue = 'set return value test';
Expand Down Expand Up @@ -186,6 +188,18 @@ namedTest('computed', 'function syntax: resolves array [] keys', function(assert
assert.deepEqual(getCallback.args[1], ['123']);
});

namedTest('computed', 'function syntax: resolves ArrayProxy []', function(assert) {
compute({
baseClass: ArrayProxy,
computed: computed('[]', getCallback),
properties: {
content: emberA(['123'])
}
});

assert.deepEqual(getCallback.args[1][0].toArray(), ['123']);
});

namedTest('computed', 'function syntax: resolves array @each keys', function(assert) {
compute({
computed: computed('[email protected]', getCallback),
Expand All @@ -197,6 +211,18 @@ namedTest('computed', 'function syntax: resolves array @each keys', function(ass
assert.deepEqual(getCallback.args[1], ['123']);
});

namedTest('computed', 'function syntax: resolves ArrayProxy @each', function(assert) {
compute({
baseClass: ArrayProxy,
computed: computed('@each.key1', getCallback),
properties: {
content: emberA([{ key1: '123' }])
}
});

assert.deepEqual(getCallback.args[1][0].toArray(), [{ key1: '123' }]);
});

namedTest('computed', 'function syntax: expands properties', function(assert) {
compute({
computed: computed('{key1,key2}', getCallback),
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/collapse-key-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,41 @@ test('it collapses array.[]', function(assert) {
);
});

test('it collapses []', function(assert) {
assert.deepEqual(
collapseKey('[]'),
['']
);
});

test('it collapses array.@each', function(assert) {
assert.deepEqual(
collapseKey('[email protected]'),
['foo']
);
});

test('it collapses @each', function(assert) {
assert.deepEqual(
collapseKey('@each.bar'),
['']
);
});

test('it collapses array.@each with brace expansion', function(assert) {
assert.deepEqual(
collapseKey('foo.@each.{bar,baz}'),
['foo']
);
});

test('it collapses @each with brace expansion', function(assert) {
assert.deepEqual(
collapseKey('@each.{bar,baz}'),
['']
);
});

test('it ignores string without brace expansion', function(assert) {
assert.deepEqual(
collapseKey('foo'),
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/get-value-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ module('Unit | get value');

assert.strictEqual(value, false);
});

test('returns the context if the macro is blank', function(assert) {
let context = {
testKey: 'test value'
};

let value = getValue({ context, macro: '' });

assert.strictEqual(value, context);
});
});

namedTest('getValue', 'returns undefined if property doesn\'t exist', function(assert) {
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/normalize-array-key-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ test('it does not alter array.[] with empty keys', function(assert) {
assert.strictEqual(result, 'foo.bar.[]');
});

test('it does not alter []', function(assert) {
let result = normalizeArrayKey('[]');

assert.strictEqual(result, '[]');
});

test('it does not alter [] with empty keys', function(assert) {
let result = normalizeArrayKey('[]', []);

assert.strictEqual(result, '[]');
});

test('it does not alter [email protected]', function(assert) {
let result = normalizeArrayKey('[email protected]');

Expand All @@ -69,6 +81,18 @@ test('it does not alter [email protected] with empty keys', function(assert) {
assert.strictEqual(result, '[email protected]');
});

test('it does not alter @each.prop', function(assert) {
let result = normalizeArrayKey('@each.prop');

assert.strictEqual(result, '@each.prop');
});

test('it does not alter @each.prop with empty keys', function(assert) {
let result = normalizeArrayKey('@each.prop', []);

assert.strictEqual(result, '@each.prop');
});

test('it watches array prop when raw array', function(assert) {
let result = normalizeArrayKey('foo.bar', ['prop']);

Expand Down

0 comments on commit 8a9f33a

Please sign in to comment.