主干:LayoutView需要在CollectionView内的ItemView中列出
Backbone: LayoutView needs to listenTo a ItemView inside a CollectionView
我正在尝试使用Backbone中的listenTo方法来侦听子视图、集合视图以及集合视图父LayoutView中的触发器。
从谷歌搜索中,很多人提到在主干网中使用嵌套对象库,但我正在努力找出标准的方法
更清楚地说,我的问题是:如何使子视图(ItemDetailsView)中的触发器冒泡到父LayoutView(MyItemsList.Layout)
var ItemDetailsView = Backbone.Marionette.LayoutView.extend({
template: JST["items/item"],
tagName: "li",
className: "item",
events: {
"click @ui.btn": "callTrigger"
},
callTrigger: function() {
this.trigger("hello:world");
}
)};
var ItemListView = Backbone.Marionette.CollectionView.extend({
tagName: "ul",
childView: itemDetailsView
});
MyItemsList.Layout = Marionette.LayoutView.extend({
template: JST["items/current-items"],
tagName: "section",
className: "current-items",
onShow: function() {
var listCollection = this.model.currentListCollection;
var listView = new MyListView({
collection: listCollection
});
this.listenTo(listView.collection, "hello:world", _.bind(function() {
console.log("I heard that!")
}, this));
}
});
使用CollectionView的childEvents属性(http://marionettejs.com/docs/v2.3.2/marionette.collectionview.html#collectionviews-childevents)。
然后,您的代码可以编写如下。
var ItemDetailsView = Backbone.Marionette.LayoutView.extend({
template: JST["items/item"],
tagName: "li",
className: "item",
events: {
"click @ui.btn": "callTrigger"
},
callTrigger: function() {
this.trigger("hello:world");
}
)};
var ItemListView = Backbone.Marionette.CollectionView.extend({
tagName: "ul",
childView: itemDetailsView,
// This callback will be called whenever a child is rendered or emits a `render` event
childEvents: {
"hello:world": function() {
console.log("a childView said hello world!");
this.triggerMethod('child:hello:world');
}
}
});
MyItemsList.Layout = Marionette.LayoutView.extend({
template: JST["items/current-items"],
tagName: "section",
className: "current-items",
onShow: function() {
var listCollection = this.model.currentListCollection;
var listView = new MyListView({
collection: listCollection
});
this.listenTo(listView, "child:hello:world", _.bind(function() {
console.log("I heard that!")
}, this));
}
});
相关文章:
- ng视图外的链接重定向到ng视图内的页面
- $ionicplatform内的$scope不;不起作用
- setTimeout可以与闭包内的函数一起使用吗
- 如何在php中的jquery中对循环内的选择框值求和
- href属性内的javascript函数
- 注册OpenLayers事件时,即使使用匿名函数或绑定,JavaScript关闭也会触发循环内的所有内容
- 对角度数据表中括号内的数字进行排序
- Angularjs使用“;这个“;promise内的关键字回调
- Angularjs无法将单选按钮与嵌套范围内的模型绑定
- 如何使用D3生成特定范围内的随机颜色
- ng模型内的ng重复的ng重复开始
- 在android中显示固定高度的webview内的大型内容
- AEM(cq5)使用组件内的信息
- 如何调用表td标记内的Onload函数
- 收集一段时间内的EMG数据.建议JS
- 使用正则表达式选择圆括号内的内容
- 替换p标记内的选定文本
- 向page.includeJs()和page.eevaluate()内的匿名函数传递参数
- 如何在d3.js中填充svg圆圈内的图像
- 主干:LayoutView需要在CollectionView内的ItemView中列出