【问题标题】:Do we need different events for each layout?每个布局需要不同的事件吗?
【发布时间】:2013-02-01 17:17:43
【问题描述】:

我的渲染方法在 _initializeLayout 之前被调用:

var _initializeLayout = function() {

    console.log('initializeLayout...');

    Controller.layout = new Layout();

        Controller.layout.on("show", function() {

        vent.trigger("layout:rendered");

    });

    vent.trigger('app:show', Controller.layout);

};

我在on渲染方法中使用布局:

// controller attach a sub view/ search View
vent.on("layout:rendered", function() {
    console.log('layout:rendered =>StartController');

    // render views for the existing HTML in the template, and attach it to the layout (i.e. don't double render)
    var inspectorStartView = new InspectorStartView();
    Controller.layout.inspector.attachView(inspectorStartView);

    var playerStartView = new PlayerStartView();
    Controller.layout.player.attachView(playerStartView);
});

当我尝试它时,我的渲染回调在 _initializeLayout() 之前被调用。我让它在路由器/控制器方法中调用 _initializeLayout:

Controller.go_inspector_control_center = function(term) {
        _initializeLayout();
        //vent.trigger("search:term", term);
    };

我刚刚再次运行它,发现正在从另一个控制器的 _initializeLayout() 方法触发一个事件:

   // private
var _initializeLayout = function() {
    console.log('initialize Start Layout...');
    Controller.layout = new Layout();
    Controller.layout.on("show", function() {
        **vent.trigger("layout:rendered"); // <--**
    });
    vent.trigger('app:show', Controller.layout);
};

看来事件需要有唯一的名称。我会试试的。如果有人知道,请插话。

安德鲁

【问题讨论】:

    标签: backbone.js marionette


    【解决方案1】:

    你不能把我们这些 Java 人带到任何地方。答案(在 Javascript 中)是我确实需要唯一地命名我的事件。当您考虑它时,这是有道理的。

    尽管我使用过的大多数事件系统都使用 X11/Java,但您不会使用它们为您命名的事件。

    这是stackoverflow question on event naming.

    我希望我的公开学习能帮助他人。 ;-)

    【讨论】:

    • 这个例子出自木偶先生本人:MyApp.vent.on("some:namespaced:event", function(){ // do stuff });在此示例中,您似乎创建了自己的命名空间,但右端的事件是事件名称。 "namespace" == some.namespaced 你可以想出你想要的 MyApp.vent.trigger("some:namespaced:event");
    猜你喜欢
    • 1970-01-01
    • 2016-08-29
    • 2018-09-11
    • 1970-01-01
    • 1970-01-01
    • 2012-09-12
    • 2012-06-21
    • 1970-01-01
    • 2011-07-30
    相关资源
    最近更新 更多