Skip to content

Commit

Permalink
Merge pull request #11861 from rwjblue/duplicate-keys
Browse files Browse the repository at this point in the history
[BUGFIX release] Update HTMLBars to allow duplicate {{each}} keys.
  • Loading branch information
rwjblue committed Jul 22, 2015
2 parents aa97f84 + 3204d4e commit 4ff7ba7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 24 deletions.
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');
});

0 comments on commit 4ff7ba7

Please sign in to comment.