Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX release] Update HTMLBars to allow duplicate {{each}} keys. #11861

Merged
merged 2 commits into from
Jul 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"finalhandler": "^0.4.0",
"github": "^0.2.3",
"glob": "~4.3.2",
"htmlbars": "0.13.33",
"htmlbars": "0.13.34",
"qunit-extras": "^1.3.0",
"qunitjs": "^1.16.0",
"route-recognizer": "0.1.5",
Expand Down
8 changes: 1 addition & 7 deletions packages/ember-htmlbars/lib/helpers/each.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Error from 'ember-metal/error';
import shouldDisplay from 'ember-views/streams/should_display';
import decodeEachKey from 'ember-htmlbars/utils/decode-each-key';

Expand Down Expand Up @@ -74,14 +73,9 @@ export default function eachHelper(params, hash, blocks) {
var keyPath = hash.key;

if (shouldDisplay(list)) {
let seenKeys = {};
forEach(list, (item, i) => {
var key = decodeEachKey(item, keyPath, i);
if (seenKeys[key] === true) {
throw new Error(`Duplicate key found ('${key}') for '{{each}}' helper, please use a unique key or switch to '{{#each model key="@index"}}{{/each}}'.`);
} else {
seenKeys[key] = true;
}

blocks.template.yieldItem(key, [item, i]);
});
} else if (blocks.inverse.yield) {
Expand Down
66 changes: 50 additions & 16 deletions packages/ember-htmlbars/tests/helpers/each_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -759,22 +759,19 @@ QUnit.test('can specify `@identity` to represent mixed object and primitive item
equal(view.$().text(), 'foobarbaz');
});

QUnit.test('duplicate keys trigger a useful error (temporary until we can deal with this properly in HTMLBars)', function() {
QUnit.test('duplicate keys work properly with primitive items', function() {
runDestroy(view);
view = EmberView.create({
items: ['a', 'a', 'a'],
template: compile('{{#each view.items as |item|}}{{item}}{{/each}}')
});

throws(
function() {
runAppend(view);
},
`Duplicate key found ('a') for '{{each}}' helper, please use a unique key or switch to '{{#each model key="@index"}}{{/each}}'.`
);
runAppend(view);

equal(view.$().text(), 'aaa');
});

QUnit.test('pushing a new duplicate key will trigger a useful error (temporary until we can deal with this properly in HTMLBars)', function() {
QUnit.test('pushing a new duplicate key will render properly with primitive items', function() {
runDestroy(view);
view = EmberView.create({
items: A(['a', 'b', 'c']),
Expand All @@ -783,12 +780,49 @@ QUnit.test('pushing a new duplicate key will trigger a useful error (temporary u

runAppend(view);

throws(
function() {
run(function() {
view.get('items').pushObject('a');
});
},
`Duplicate key found ('a') for '{{each}}' helper, please use a unique key or switch to '{{#each model key="@index"}}{{/each}}'.`
);
run(function() {
view.get('items').pushObject('a');
});

equal(view.$().text(), 'abca');
});

QUnit.test('duplicate keys work properly with objects', function() {
runDestroy(view);
let duplicateItem = { display: 'foo' };
view = EmberView.create({
items: [
duplicateItem,
duplicateItem,
{ display: 'bar' },
{ display: 'qux' }
],
template: compile('{{#each view.items as |item|}}{{item.display}}{{/each}}')
});

runAppend(view);

equal(view.$().text(), 'foofoobarqux');
});

QUnit.test('pushing a new duplicate key will render properly with objects', function() {
runDestroy(view);

let duplicateItem = { display: 'foo' };
view = EmberView.create({
items: A([
duplicateItem,
{ display: 'bar' },
{ display: 'qux' }
]),
template: compile('{{#each view.items as |item|}}{{item.display}}{{/each}}')
});

runAppend(view);

run(function() {
view.get('items').pushObject(duplicateItem);
});

equal(view.$().text(), 'foobarquxfoo');
});