質問
が考えら眺めることを定めるとともに、リストのオブジェクト
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
}
})
のではないでしょうかかいしていただくためにイベントの親会社、特に次の場合には、親ビューが設定されていないにも知られている児童のとおりです。していると理解しているが、これまでの取得物件 foo
のparentViewのいずれか this.getPath('parentView').get('foo')
または this.getPath('contentView').get('foo')
.聞いても機能(この場合、 itemclicked()
)?
解決
this.get('parentView').itemClicked(this.get('item'));
トリックをする必要があります。
他のヒント
で利用できます {{action}}
ヘルパー、 http://jsfiddle.net/smvv5/
テンプレート:
<script type="text/x-handlebars" >
{{#view App.ListsView}}
{{#each items}}
{{#view App.ListView itemBinding="this" }}
<li {{action "clicked" target="parentView" }} >{{item.text}}</li>
{{/view}}
{{/each}}
{{/view}}
</script>
JS:
App = Ember.Application.create({});
App.Foo = Ember.ArrayProxy.create({
content: [Ember.Object.create({
text: 'hello'
}), Ember.Object.create({
text: 'action'
}), Ember.Object.create({
text: 'world'
})]
});
App.ListsView = Ember.View.extend({
itemsBinding: 'App.Foo',
clicked: function(view, event, ctx) {
console.log(Ember.getPath(ctx, 'item.text'));
}
});
App.ListView = Ember.View.extend({
});
Emberの最近のバージョンは使用します actions
オブジェクト上のメソッドの代わりにハッシュ(この非推奨方法はまだサポートされていますが、長くはないかもしれません)。ハンドラーに渡されたビューへの参照が必要な場合は、「ビュー」をパラメーターとして送信して使用します。 parentView
ターゲットとして。
<button {{action "onClicked" view target="view.parentView"}}>Click me.</button>
App.ListsView = Ember.View.extend({
actions: {
onClicked: function(view) {
}
}
});
{{action}}
ヘルパーはイベントオブジェクトを送信しません。イベントが必要な場合は、イベントへの参照を取得する方法がまだわかりません。
所属していません StackOverflow