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

i'm playing with Marionette first time. After re-rendering ItemViews, their events not triggered.

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");

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745189596a4615802.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信