1

我在子应用程序模块中遇到了一些 Backbone Marionette 控制器的奇怪问题。

我无法使用“controller.listenTo(view, 'event', ...)” 方法捕获其视图事件之一,尽管“view.on('event',...)” 不起作用问题。

这是模块视图的示例代码:

MyApp.module("SubApp.Selector", function (Selector, App, Backbone, Marionette, $, _) {
    "use strict";

    // Category Selector View
    // ------------------
    // Display a list of categories in the selector

    // Categories list item
    Selector.CategoryOption = Marionette.ItemView.extend({
        template:   "#my-category-template",
        tagName:        "option",
        onRender: function () { this.$el.attr('value', this.model.get('id')); }
    });

    // Categories list container
    Selector.CategoryListView = Marionette.CompositeView.extend({
        template:   "#my-category-list-template",
        tagName:        "div",
        id:     "categories",
        itemView:   Selector.CategoryOption,
        itemViewContainer: "select",
        events: {
            'change #category_items': 'categoryClicked'
        },
        categoryClicked: function() {
            var catID = this.$('#category_items').val();
            console.log("category clicked "+catID);
            this.trigger("category:changed", catID);
        }
    });

    // Selector Component Controller
    // -------------------------------
    Selector.Viewer = Marionette.Controller.extend({
        initialize: function (_options) {
            this.region = _options.region;
            this.collection = _options.collection;
        },
        show: function () {
            var view = new Selector.CategoryListView({ collection: this.collection });
            this.listenTo(view, 'category:changed', this.categoryChanged);
            //view.on('category:changed', this.categoryChanged, this);
            this.region.show(view);
        },
        categoryChanged: function (_category) {
            console.log("category changed in controller");
        }
    });
});

从控制器对象监听事件我有什么问题吗?

我不应该像广泛推荐的那样使用 listenTo 语法来进行正确的事件处理和侦听器破坏吗?

4

0 回答 0