2012-06-28 36 views
0

这里是我使用它创建树结构的代码compositeView代码片段。骨干牵线木偶复合视图没有触发事件

var TreeView = Backbone.Marionette.CompositeView.extend({ 

    template: "#filterTemplate", 
    className:"menuItem", 
    tagName: "ul", 

    initialize: function(){ 
     this.collection = this.model.type; 
     counter=0; 
    }, 

    events: { 
     'click .menuItem': 'show' 
    }, 

    show: function(event) { 
     var target = $(event.target); 
     console.log(target); 

    }, 

    appendHtml: function(collectionView, itemView){ 
     // ensure we nest the child list inside of 
     // the current list item 
     $(itemView.el).attr("id","innerMenu"+counter); 
     $(itemView.el).attr("class","innerMenu"); 
     collectionView.$("li:first").append(itemView.el); 
     counter++; 
    } 
}); 

树呈现完美,但事件没有被绑定或不被解雇。 Show方法从不被调用。我正在使用Backbone.Marionette v0.9.1

回答

6

您已将视图本身设置为使用menuItem css类进行渲染。在任何骨干视图(这不是专用于Marionette)中,如果您想直接处理视图元素上的事件(不是其中一个子视图),则可以指定没有选择器的事件。

在你的情况,这将是:

events: { 
    "click": "show" 
} 

这将配置当你点击这个视图的HTML的任何部分视图的el直接“点击”事件和show方法将被调用。

+0

谢谢derick..it是有帮助的:)我想知道如果我只能添加事件到主菜单而不是子菜单项。 – tousif