diff --git a/packages/ember-htmlbars/lib/keywords/get.js b/packages/ember-htmlbars/lib/keywords/get.js index da5fde73c28..223e24c5cb5 100644 --- a/packages/ember-htmlbars/lib/keywords/get.js +++ b/packages/ember-htmlbars/lib/keywords/get.js @@ -5,7 +5,6 @@ import { assert } from 'ember-metal/debug'; import BasicStream from 'ember-metal/streams/stream'; -import KeyStream from 'ember-metal/streams/key-stream'; import { isStream } from 'ember-metal/streams/utils'; import subscribe from 'ember-htmlbars/utils/subscribe'; import { get } from 'ember-metal/property_get'; @@ -23,9 +22,7 @@ function labelFor(source, key) { let DynamicKeyStream = BasicStream.extend({ init(source, keySource) { - assert('DynamicKeyStream error: source must be a stream', isStream(source)); // TODO: This isn't necessary. - - // used to get the original path for debugging and legacy purposes + // used to get the original path for debugging purposes var label = labelFor(source, keySource); this.label = label; @@ -39,7 +36,6 @@ let DynamicKeyStream = BasicStream.extend({ key() { const key = this.keyDep.getValue(); if (typeof key === 'string') { - assert('DynamicKeyStream error: key must not have a \'.\'', key.indexOf('.') === -1); return key; } }, @@ -100,7 +96,7 @@ const buildStream = function buildStream(params) { function buildDynamicKeyStream(source, keySource) { if (!isStream(keySource)) { - return new KeyStream(source, keySource); + return source.get(keySource); } else { return new DynamicKeyStream(source, keySource); } @@ -148,7 +144,7 @@ function buildDynamicKeyStream(source, keySource) { @method get @for Ember.Templates.helpers */ -var getKeyword = function getKeyword(morph, env, scope, params, hash, template, inverse, visitor) { +function getKeyword(morph, env, scope, params, hash, template, inverse, visitor) { if (morph === null) { return buildStream(params); } else { @@ -167,6 +163,6 @@ var getKeyword = function getKeyword(morph, env, scope, params, hash, template, } return true; -}; +} export default getKeyword; diff --git a/packages/ember-htmlbars/tests/helpers/get_test.js b/packages/ember-htmlbars/tests/helpers/get_test.js index 657636c99c5..ccc5a760f34 100644 --- a/packages/ember-htmlbars/tests/helpers/get_test.js +++ b/packages/ember-htmlbars/tests/helpers/get_test.js @@ -55,6 +55,33 @@ QUnit.test('should be able to get an object value with a static key', function() equal(view.$().text(), '[red] [red]', 'should return \'red\' for {{get colors \'apple\'}}'); }); +QUnit.test('should be able to get an object value with nested static key', function() { + var context = { + colors: { apple: { gala: 'red and yellow' }, banana: 'yellow' } + }; + + view = EmberView.create({ + context: context, + template: compile(`[{{get colors "apple.gala"}}] [{{if true (get colors "apple.gala")}}]`) + }); + + runAppend(view); + + equal(view.$().text(), '[red and yellow] [red and yellow]', 'should return \'red and yellow\' for {{get colors "apple.gala"}}'); + + run(function() { + view.set('context.colors', { apple: { gala: 'yellow and red striped' }, banana: 'purple' }); + }); + + equal(view.$().text(), '[yellow and red striped] [yellow and red striped]', 'should return \'yellow and red striped\' for {{get colors \'apple.gala\'}}'); + + run(function() { + view.set('context.colors.apple.gala', 'yellow-redish'); + }); + + equal(view.$().text(), '[yellow-redish] [yellow-redish]', 'should return \'yellow-redish\' for {{get colors \'apple.gala\'}}'); +}); + QUnit.test('should be able to get an object value with a bound/dynamic key', function() { var context = { colors: { apple: 'red', banana: 'yellow' }, @@ -95,6 +122,61 @@ QUnit.test('should be able to get an object value with a bound/dynamic key', fun equal(view.$().text(), '[red] [red]', 'should return \'red\' for {{get colors key}} (key = \'apple\')'); }); +QUnit.test('should be able to get an object value with nested dynamic key', function() { + var context = { + colors: { apple: { gala: 'red and yellow', mcintosh: 'red' }, banana: 'yellow' }, + key: 'apple.gala' + }; + + view = EmberView.create({ + context: context, + template: compile('[{{get colors key}}] [{{if true (get colors key)}}]') + }); + + runAppend(view); + + equal(view.$().text(), '[red and yellow] [red and yellow]', 'should return \'red and yellow\' for {{get colors "apple.gala"}}'); + + run(function() { + view.set('context.key', 'apple.mcintosh'); + }); + + equal(view.$().text(), '[red] [red]', 'should return \'red\' for {{get colors \'apple.mcintosh\'}}'); + + run(function() { + view.set('context.key', 'banana'); + }); + + equal(view.$().text(), '[yellow] [yellow]', 'should return \'yellow\' for {{get colors \'banana\'}}'); +}); + +QUnit.test('should be able to get an object value with subexpression returning nested key', function() { + var context = { + colors: { apple: { gala: 'red and yellow', mcintosh: 'red' }, banana: 'yellow' } + }; + + view = EmberView.create({ + context: context, + template: compile(`[{{get colors (concat 'apple' '.' 'gala')}}] [{{if true (get colors (concat 'apple' '.' 'gala'))}}]`) + }); + + runAppend(view); + + equal(view.$().text(), '[red and yellow] [red and yellow]', 'should return \'red and yellow\' for {{get colors "apple.gala"}}'); + + run(function() { + view.set('context.colors', { apple: { gala: 'yellow and red striped' }, banana: 'purple' }); + }); + + equal(view.$().text(), '[yellow and red striped] [yellow and red striped]', 'should return \'yellow and red striped\' for {{get colors \'apple.gala\'}}'); + + run(function() { + view.set('context.colors.apple.gala', 'yellow-redish'); + }); + + equal(view.$().text(), '[yellow-redish] [yellow-redish]', 'should return \'yellow-redish\' for {{get colors \'apple.gala\'}}'); +}); + QUnit.test('should be able to get an object value with a GetStream key', function() { var context = { colors: { apple: 'red', banana: 'yellow' }, @@ -344,6 +426,41 @@ QUnit.test('get helper value should be updatable using {{input}} and (mut) - dyn equal(view.get('context.source.banana'), 'some value'); }); +QUnit.test('get helper value should be updatable using {{input}} and (mut) - dynamic nested key', function() { + var context = { + source: Ember.Object.create({ + apple: { + mcintosh: 'mcintosh' + } + }), + key: 'apple.mcintosh' + }; + + view = EmberView.create({ + context: context, + container: container, + template: compile(`{{input type='text' value=(mut (get source key)) id='get-input'}}`) + }); + + runAppend(view); + + equal(view.$('#get-input').val(), 'mcintosh'); + + run(function() { + view.set('context.source.apple.mcintosh', 'red'); + }); + + equal(view.$('#get-input').val(), 'red'); + + run(function() { + view.$('#get-input').val('some value'); + view.childViews[0]._elementValueDidChange(); + }); + + equal(view.$('#get-input').val(), 'some value'); + equal(view.get('context.source.apple.mcintosh'), 'some value'); +}); + QUnit.test('get helper value should be updatable using {{input}} and (mut) - static key', function() { var context = { source: Ember.Object.create({