考慮一個視圖定義對象的列表:與模板如何將事件傳遞給父視圖,傳遞觸發事件的子視圖?
App.ListView = Ember.View({
items: 'App.FooController.content'
itemClicked: function(item){
}
)};
:
<ul>
{{#each items}}
{{#view App.ItemView itemBinding="this" tagName="li"}}
<!-- ... -->
{{/view}}
{{/each}}
</ul>
和ItemView控件:
App.ItemView = Ember.View.extend({
click: function(event){
var item = this.get('item');
// I want to call function itemClicked(item) of parentView
// so that it handles the click event
}
})
所以基本上我的問題是如何傳遞事件到父視圖,特別是在父視圖未被子視圖所知的情況下?我知道您可以通過this.getPath('parentView').get('foo')
或this.getPath('contentView').get('foo')
獲得父視圖的屬性foo
。但是一個函數怎麼樣(在這種情況下,itemclicked()
)?
我以爲我測試,並沒有奏效。我會讓一個jsFiddle來測試它。我很確定'this.get('contentView')。itemClicked(this.get('item'));'(用於將事件傳遞給父母)不起作用。 – 2012-02-21 21:32:48
@Zack它似乎在這裏工作:http://jsfiddle.net/tomwhatmore/FGyrV/1/除非我誤解了這個問題。可能是因爲你使用'getPath()'而不是'get()'? – 2012-02-21 22:31:34
Yeap,我正在使用getPath :) – 2012-02-22 06:09:01