Skip to content

Commit

Permalink
Merge pull request #10876 from nathanhammond/master
Browse files Browse the repository at this point in the history
[BUGFIX beta] Make the component helper able to deal with dynamically set falsey values.
  • Loading branch information
mmun committed Apr 24, 2015
2 parents 567c63b + 3707d7a commit 3b8ad3f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
20 changes: 20 additions & 0 deletions packages/ember-htmlbars/tests/helpers/component_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,26 @@ if (Ember.FEATURES.isEnabled('ember-htmlbars-component-helper')) {
}, /HTMLBars error: Could not find component named "does-not-exist"./);
});

QUnit.test("component with unquoted param resolving to a component, then non-existent component", function() {
registry.register('template:components/foo-bar', compile('yippie! {{location}} {{yield}}'));
view = EmberView.create({
container: container,
dynamicComponent: 'foo-bar',
location: 'Caracas',
template: compile('{{#component view.dynamicComponent location=view.location}}arepas!{{/component}}')
});

runAppend(view);

equal(view.$().text(), 'yippie! Caracas arepas!', 'component was looked up and rendered');

Ember.run(function() {
set(view, "dynamicComponent", undefined);
});

equal(view.$().text(), '', 'component correctly deals with falsey values set post-render');
});

QUnit.test("component with quoted param for non-existent component", function() {
view = EmberView.create({
container: container,
Expand Down
22 changes: 12 additions & 10 deletions packages/ember-views/lib/views/bound_component_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,35 @@
*/

import { _Metamorph } from "ember-views/views/metamorph_view";
import { read, chain, subscribe, unsubscribe } from "ember-metal/streams/utils";
import { read, subscribe, unsubscribe } from "ember-metal/streams/utils";
import { readComponentFactory } from "ember-views/streams/utils";
import mergeViewBindings from "ember-htmlbars/system/merge-view-bindings";
import EmberError from "ember-metal/error";
import ContainerView from "ember-views/views/container_view";
import View from "ember-views/views/view";

export default ContainerView.extend(_Metamorph, {
init() {
this._super(...arguments);
var componentNameStream = this._boundComponentOptions.componentNameStream;
var container = this.container;
this.componentClassStream = chain(componentNameStream, function() {
return readComponentFactory(componentNameStream, container);
});
this.componentNameStream = this._boundComponentOptions.componentNameStream;

subscribe(this.componentClassStream, this._updateBoundChildComponent, this);
subscribe(this.componentNameStream, this._updateBoundChildComponent, this);
this._updateBoundChildComponent();
},
willDestroy() {
unsubscribe(this.componentClassStream, this._updateBoundChildComponent, this);
unsubscribe(this.componentNameStream, this._updateBoundChildComponent, this);
this._super(...arguments);
},
_updateBoundChildComponent() {
this.replace(0, 1, [this._createNewComponent()]);
},
_createNewComponent() {
var componentClass = read(this.componentClassStream);
var componentName = read(this.componentNameStream);
if (!componentName) {
return this.createChildView(View);
}

var componentClass = readComponentFactory(componentName, this.container);
if (!componentClass) {
throw new EmberError('HTMLBars error: Could not find component named "' + read(this._boundComponentOptions.componentNameStream) + '".');
}
Expand All @@ -39,7 +41,7 @@ export default ContainerView.extend(_Metamorph, {

var prop;
for (prop in hash) {
if (prop === '_boundComponentOptions' || prop === 'componentClassStream') { continue; }
if (prop === '_boundComponentOptions' || prop === 'componentNameStream') { continue; }
hashForComponent[prop] = hash[prop];
}

Expand Down

0 comments on commit 3b8ad3f

Please sign in to comment.