最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Marionette ItemView events after re-rendering - Stack Overflow

programmeradmin2浏览0评论

i'm playing with Marionette first time. After re-rendering ItemViews, their events not triggered. 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. What i'm doing wrong?

i'm playing with Marionette first time. After re-rendering ItemViews, their events not triggered. 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. What i'm doing wrong?

Share Improve this question asked Jul 16, 2013 at 23:23 iBoozyVoozyiBoozyVoozy 777 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

The problem you are having is that region.show() is going to close any view that is currently occupying that region. Doing so undelegates the view's events. After the initial region.show() you should manually call render on the view.

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

instead of using eva to listen to events that happen on the views, try listening to eva for events passed by other views

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

var eva = self.vent;
eva.trigger("someStringHere");
发布评论

评论列表(0)

  1. 暂无评论