简体   繁体   English

重新渲染后的Marionette ItemView事件

[英]Marionette ItemView events after re-rendering

i'm playing with Marionette first time. 我第一次和Marionette一起玩。 After re-rendering ItemViews, their events not triggered. 重新渲染ItemViews后,不会触发其事件。 Simple example: 简单的例子:

App = new Marionette.Application;

App.addRegions({
    headerRegion: '#header',
    contentRegion: '#content',
});

App.addInitializer(function () {
    this.Views = {
        MainMenu : new MainMenuView(),
        ContentOne : new ContentOneView(),
        ContentTwo : new ContentTwoView(),
    };
});

App.addInitializer(function () {
    var self = this;
    var eva = self.vent;
    eva.listenTo(self.Views.MainMenu, 'content1', function () {
        self.contentRegion.show(self.Views.ContentOne);
    });
    eva.listenTo(self.Views.MainMenu, 'content2', function () {
        self.contentRegion.show(self.Views.ContentTwo);
    });
});

App.on('start', function () {
    var self = this;
    self.contentRegion.show(self.View.ContentOne);
});

App.start();

After re-rendering ContentOneView & ContentTwoView, their events not triggered. 重新渲染ContentOneView和ContentTwoView后,不会触发其事件。 What i'm doing wrong? 我做错了什么?

The problem you are having is that region.show() is going to close any view that is currently occupying that region. 您遇到的问题是region.show()将关闭当前占用该区域的任何视图。 Doing so undelegates the view's events. 这样做会取消视图的事件。 After the initial region.show() you should manually call render on the view. 在初始region.show()之后,您应该在视图上手动调用render。

You can see this explained here and an issue discussing it here . 您可以在此处看到此解释,在此处讨论此问题。

I managed to solve this problem by using delegating the events when the view is shown in the layout: 我设法通过在布局中显示视图时委派事件来解决此问题:

layoutView.content.show(contentView);
contentView.delegateEvents();

Although this is only necessary after the first render as mentioned by Andrew Hubbs 虽然这只是在Andrew Hubbs提到的第一次渲染之后才需要

instead of using eva to listen to events that happen on the views, try listening to eva for events passed by other views 而不是使用eva来监听视图上发生的事件,尝试听eva以获取其他视图传递的事件

App.addInitializer(function () {
    var eva = self.vent;
    var self = this;
    this.listenTo(eva, 'someStringHere', function(){/*do stuff here*/};
});

and then in your views you can trigger events through eva/vent 然后在你的视图中,你可以通过eva / vent触发事件

var eva = self.vent;
eva.trigger("someStringHere");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM