Search code examples
javascriptbackbone.jsmarionette

Call `show` on Marionette LayoutView region


I have this Layout View:

var appLayoutView = Backbone.Marionette.LayoutView.extend({
    template: function() {
        return "some template string";
    },
    regions: {
        notify: "[data-region='Notify']"
    },
    onShow: function() {
        this.regions.notify.show(new notifyView());
    }
});

Which I call like so:

mainLayout.app.show(appLayout);

So ideally, I'd like, when I run the above line (essentially when the layout view is put into the DOM) for the notifyView to be rendered into the "notify" region. However this.regions.notify is just a string. How can I achieve what I'm trying to do here? Basically having the render logic for "notify" inside the Layout View class, and not controlled from the invocation line.


Solution

  • I can't find any docs that show where this got added, but LayoutView should have a getRegion method : https://github.com/marionettejs/backbone.marionette/blob/master/src/marionette.layoutview.js#L74

    so your code would look like :

    var appLayoutView = Backbone.Marionette.LayoutView.extend({
        template: function() {
            return "some template string";
        },
        regions: {
            notify: "[data-region='Notify']"
        },
        onShow: function() {
            this.getRegion('notify').show(new notifyView());
        }
    });