Skip to content

Commit

Permalink
Fix components inside group helper
Browse files Browse the repository at this point in the history
Components (like `link-to`) end up with the wrong context when used
inside a `#group` helper. And their templates don't get grouped.

I understand that #group is experimental, but as long as the support for
it remains in master, it makes sense to not leave it broken.
  • Loading branch information
ef4 committed Jun 3, 2014
1 parent 6555b15 commit 6d9a2e5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
6 changes: 6 additions & 0 deletions packages/ember-handlebars/lib/helpers/binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ function bind(property, options, preserveContext, shouldDisplay, valueNormalizer
if ('object' === typeof this) {
if (data.insideGroup) {
observer = function() {
while (view._contextView) {
view = view._contextView;
}
run.once(view, 'rerender');
};

Expand Down Expand Up @@ -197,6 +200,9 @@ function simpleBind(currentContext, property, options) {
if (pathRoot && ('object' === typeof pathRoot)) {
if (data.insideGroup) {
observer = function() {
while (view._contextView) {
view = view._contextView;
}
run.once(view, 'rerender');
};

Expand Down
20 changes: 20 additions & 0 deletions packages/ember-handlebars/tests/helpers/group_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { A } from "ember-runtime/system/native_array";
import Container from "ember-runtime/system/container";
import { get } from "ember-metal/property_get";
import { set } from "ember-metal/property_set";
import Component from "ember-views/views/component";

var trim = jQuery.trim;
var container, view;
Expand Down Expand Up @@ -226,6 +227,25 @@ test("an #each can be nested with a view inside", function() {
equal(view.$().text(), 'ErikTom', "The updated object's view was rerendered");
});

test("an #each can be nested with a component inside", function() {
var yehuda = {name: 'Yehuda'};
container.register('view:test', Component.extend());
createGroupedView(
'{{#each people}}{{#view "test"}}{{name}}{{/view}}{{/each}}',
{people: A([yehuda, {name: 'Tom'}])}
);

appendView();
equal(view.$('script').length, 0, "No Metamorph markers are output");
equal(view.$().text(), 'YehudaTom', "The content was rendered");

run(function() {
set(yehuda, 'name', 'Erik');
});

equal(view.$().text(), 'ErikTom', "The updated object's view was rerendered");
});

test("#each with groupedRows=true behaves like a normal bound #each", function() {
createGroupedView(
'{{#each numbers groupedRows=true}}{{this}}{{/each}}',
Expand Down
5 changes: 3 additions & 2 deletions packages/ember-views/lib/views/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ var Component = View.extend(TargetActionSupport, ComponentTemplateDeprecation, {

init: function() {
this._super();
set(this, 'origContext', get(this, 'context'));
set(this, 'context', this);
set(this, 'controller', this);
},
Expand Down Expand Up @@ -180,9 +181,9 @@ var Component = View.extend(TargetActionSupport, ComponentTemplateDeprecation, {
tagName: '',
_contextView: parentView,
template: template,
context: get(parentView, 'context'),
context: options.data.insideGroup ? get(this, 'origContext') : get(parentView, 'context'),
controller: get(parentView, 'controller'),
templateData: { keywords: parentView.cloneKeywords() }
templateData: { keywords: parentView.cloneKeywords(), insideGroup: options.data.insideGroup }
});
}
},
Expand Down

0 comments on commit 6d9a2e5

Please sign in to comment.