Skip to content

Commit

Permalink
Merge pull request #12323 from rwjblue/make-nested-paths-work-with-ge…
Browse files Browse the repository at this point in the history
…t-helper

[BUGFIX beta] Make `{{get something 'path.goes.here'}}` work.
  • Loading branch information
rwjblue committed Sep 10, 2015
2 parents c665584 + 4c124c3 commit c5774e3
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 8 deletions.
12 changes: 4 additions & 8 deletions packages/ember-htmlbars/lib/keywords/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand All @@ -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;
}
},
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 {
Expand All @@ -167,6 +163,6 @@ var getKeyword = function getKeyword(morph, env, scope, params, hash, template,
}

return true;
};
}

export default getKeyword;
117 changes: 117 additions & 0 deletions packages/ember-htmlbars/tests/helpers/get_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand Down Expand Up @@ -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' },
Expand Down Expand Up @@ -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({
Expand Down

0 comments on commit c5774e3

Please sign in to comment.