将多个函数绑定到视图的单击事件

时间:2014-02-18 18:00:17

标签: backbone.js marionette

我正在尝试将一些函数绑定到click事件。这就是我所拥有的:

var Foreground = Backbone.Marionette.Layout.extend({

    el: $('body'),

    events: {
        'click': 'tryResetContextMenu onClickDeselectCollections'
    },

    //  Whenever the user clicks on any part of the UI that isn't a multi-select item, deselect the multi-select items.
    onClickDeselectCollections: function (event) {
        console.log("deselect collections");
        var isMultiSelectItem = $(event.target).hasClass('multiSelectItem');
        var isChildMultiSelectItem = $(event.target).closest('.multiSelectItem').length > 0;

        if (!isMultiSelectItem && !isChildMultiSelectItem) {
            this.deselectCollections();
        }
    },

    //  If a click occurs and the default isn't prevented, reset the context menu groups to hide it.
    //  Child elements will call event.preventDefault() to indicate that they have handled the context menu.
    tryResetContextMenu: function (event) {
        console.log("resetting context menu items");
        if (event.isDefaultPrevented()) {

            //  TODO: I don't think this is the proper way to do this. I should be showing a new view with top/left defined?
            this.contextMenu.currentView.show({
                top: event.pageY,
                //  Show the element just slightly offset as to not break onHover effects.
                left: event.pageX + 1
            });

        } else {

            //  Clearing the groups of the context menu will cause it to become hidden.
            ContextMenuItems.reset();
        }
    }
});

这两个功能都不会触发,但可以单独使用。是否支持此功能?

1 个答案:

答案 0 :(得分:5)

这样做:

events: {
    'click': function(event) {
                 this.tryResetContextMenu(event);
                 this.onClickDeselectCollections(event);
             }
},