Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a layout that accepts a variable number of views (and hence regions)
    primarykey
    data
    text
    <p><strong>My goal</strong> I need to create a custom layout (a flow layout) that can receive a variable numbers of views and based on them, it creates regions as necessary and within those regions it shows the views that are passed in. The views can be arranged vertically or horizontally.</p> <p><strong>Requirement</strong> The layout has a template where initially regions are not defined. It only contains a wrapper (<code>data-role="region-wrapper"</code>) where added regions will be rendered.</p> <p>My approach.</p> <p>1 - Extend a <code>Marionette.Layout</code> (obviously)</p> <p>2 - Ovveride the construtor like the following</p> <pre><code>constructor: function(options) { // call super here... this.viewList= options.viewList || []; this._defineRegions(); // see 3 } </code></pre> <p>3 - Define the regions dynamically </p> <pre><code>_defineRegions: function() { _.each(this.viewList, function(view, index) { var name = 'flowRegion_' + index; var definition = { selector: "[data-region='flow-region-" + index + "']" }; this.addRegion(name, definition); }, this); }, </code></pre> <p>4 - Render regions and views in onRender method within the same layout</p> <pre><code>onRender: function() { _.each(this.viewList, function(view, index) { // if the view has not been instantiated, instantiate it // a region is a simple div element var $regionEl = // creating a region element here based on the index // append the region here this.$el.find("[data-role='flow-wrapper']").append($regionEl); var region = this.getRegion(index); // grab the correct region from this.regionManager region.show(view); }, this); } </code></pre> <p>This solution seems working but I would like to know if I'm following a valid one or not. Any other approach to follow?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload