我試圖創建一個下拉菜單組件。它由兩部分組成 - currency-dropdown (parent)
,dropdown-item (child)
。我能夠渲染組件。但是當我點擊dropdown-item
組件時,我無法觸發父組件上的操作。EmberJS觸發對子項父組件的操作
我想將選定的組件數據發送給父組件。我嘗試設置targetObject和我在這裏找到的許多其他組合。我不知道是什麼問題。我確實在父組件中擴展了_yield
,因爲我在每個幫助器中渲染子組件。一些幫助將不勝感激。 Here is what I've got so far.
App.CurrencyDropdownComponent = Ember.Component.extend({
actions: {
itemSelected: function(item) {
console.log(item);
}
},
_yield: function(content, options) {
var get = Ember.get,
view = options.data.view,
parentView = this._parentView,
template = get(this, 'template');
if (template) {
view.appendChild(Ember.View, {
isVirtual: true,
tagName: '',
_contextView: parentView,
template: template,
context: get(view, 'content'),
controller: get(parentView, 'controller'),
templateData: { keywords: parentView.cloneKeywords() }
});
}
}
});
App.DropdownItemComponent = Ember.Component.extend({
click: function() {
this.sendAction('selectItem', this.origContext);
}
});
<script type="text/x-handlebars" id="index">
<header>
{{#currency-dropdown currencies=model}}
{{#dropdown-item targetObject=view selectItem="itemSelected"}}
<span class="cdd-selected-tick">✔</span>
<span class="font-13">{{name}}</span>
<span class="push-right font-11">{{symbol}}</span>
{{/dropdown-item}}
{{/currency-dropdown}}
</header>
</script>
<script type="text/x-handlebars" id="components/currency-dropdown">
<div class="cdd-box">
<input class="cdd-input" type="hidden"/>
<div class="cdd-selected-box" {{action "toggleDropDown"}}>
<strong>Currency</strong>
<span class="uppercase"> {{currencies.0.currencyCode}} </span>
{{currencies.0.symbol}}
<div class="down-arrow"></div>
</div>
<ul class="cdd-selection-box" >
{{#each item in currencies}}
<li>{{yield}}</li>
{{/each}}
</ul>
</div>
</script>
For anyone interested I have made my solution an addon.
我得到這個工作正常通過消除所有的產量。但是,它不是真正可重用的。 http://emberjs.jsbin.com/yoheza/1/edit – blessenm 2014-11-22 09:52:06