【问题标题】:backbone marionette composite view not firing events骨干木偶复合视图未触发事件
【发布时间】:2012-06-28 09:45:11
【问题描述】:

这是我用来创建树结构的代码compositeView sn-p。

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

【问题讨论】:

    标签: backbone.js marionette


    【解决方案1】:

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

    在你的情况下,它会是:

    events: {
        "click": "show"
    }
    

    这将直接使用“click”事件配置视图的el,并且当您单击该视图的 HTML 的任何部分时将调用 show 方法。

    【讨论】:

    • 谢谢 derick.. 它很有帮助 :) 我想知道我是否只能将事件添加到主菜单而不是子菜单项。
    猜你喜欢
    • 1970-01-01
    • 2014-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-18
    • 1970-01-01
    • 2017-02-13
    • 2014-03-27
    相关资源
    最近更新 更多