Using multiple Marionette.ItemView components within a single "region"

Consider this scenario -

There are two Marionette applications, each containing an ItemView with a template in the same region. Subsequently, instances of both ItemViews are created as follows:

<script>
 ManagerMarionette1 = new Marionette.Application();
        // defining the region ...
        ManagerMarionette1.addRegions({
            mainRegion: "#mainRegion",
        });

        // ItemView definition for Marionette 1
        ManagerMarionette1.RegionItemView = Marionette.ItemView.extend({
            template: _.template('<div class="region">Marionette 1</div>'),
            onRender: function () {
                this.$('.region');
            }
        });



        ManagerMarionette2 = new Marionette.Application();
        // defining the region ...
        ManagerMarionette2.addRegions({
            mainRegion: "#mainRegion",
        });

        // ItemView definition for Marionette 2
        ManagerMarionette2.RegionItemView = Marionette.ItemView.extend({
            template: _.template('<div class="region">Marionette 2</div>'),
            onRender: function () {
                this.$('.region');
            }
        });
...
MyMarionette1 = ManagerMarionette1.RegionItemView;
        myMarionette1 = new MyMarionette1();

        MyMarionette2 = ManagerMarionette2.RegionItemView;
        myMarionette2 = new MyMarionette2();

</script>

The designated region is represented by -

<div id="mainRegion"></div>

There are two buttons for each of the Marionette ItemViews, which can be rendered by clicking them individually -

<button onclick="ManagerMarionette1.mainRegion.show(myMarionette1)">Show Marionette 1</button>
    <button onclick="ManagerMarionette2.mainRegion.show(myMarionette2);">Show Marionette 2</button>

Initially, it may seem to work fine - each button rendering its respective ItemView. However, upon multiple clicks, one button stops rendering while the other continues to render.

How can I properly manage the Marionette applications to ensure that each button renders its respective region correctly?

Edit:

A revised version based on the suggestion from @David_Sulc can be foundhere.

Answer №1

It's not a great idea to display 2 different views in the same region because typically, the second `show` call will close the currently displayed view and stop listening for events. However, in this case, using 2 different Marionette regions prevents this from happening, potentially causing memory leaks each time you switch between views.

Instead of this approach, consider utilizing sub-modules (check out https://github.com/marionettejs/backbone.marionette/blob/master/docs/marionette.application.module.md#defining-sub-modules) that can both display views within the main application.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using Three.js to establish coplanarity of points and project coplanar points onto the XY plane

Looking to determine if a set of Vector3 points are co-planar in a specific plane and then project them onto the XY plane while maintaining their scale. Any suggestions on how to accomplish this efficiently using three.js? ...

The collaboration of TypeScript and Vue in implementing index signatures resulted in the TS7053 specification

Utilizing Vue 2.6 with vue class component along with typescript. Here is the current code snippet: private validateField(fieldType: string, e: string) { this[fieldType] = e.length > 0 } An error is being thrown: Error TS7053: Element implicitly h ...

Share a URL seamlessly without the need for a page refresh or AJAX request

I am looking for a way to trigger a script to save an image to the server when a link is clicked, without actually navigating to a different page. Even though I tried using e.preventdefualt to prevent the link from submitting, it didn't work as expec ...

Real-time Calculation and Validation of Input Field Totals Using JQuery with Modulus % Rules

Hello, I need assistance with validating my input fields to accept values that are divisible by certain numbers. The criteria are as follows: inputValue % 10 == 0; inputValue % 20 == 0; inputValue % 50 == 0; inputValue % 100 == 0; inputValue % 200 == 0; ...

The POST Method is failing to transmit all the necessary data

I recently started a new project using angular, node, express, and postgresql/sequelize. It's been an exciting learning experience, but I've encountered some challenges. Specifically, I'm having trouble setting up an update route for a model ...

Renaming the month in Material-UI React

Using DatePicker from Material Ui, I am trying to change the name of the month. How can this be achieved? For instance, I need to change August to Avqust or March to Mart enter image description here This is my code: <LocalizationProvider ...

Why is my PHP code returning empty in Chrome but showing values in Firefox? What steps can I take to troubleshoot this

My initial assumption was that it should be server-side specific and not introduce differing behaviors dependent on the browser. The code snippet below results in null, but when tested in Firefox, it contains data that is later processed into a table. $H ...

To make sure that async tests and hooks are properly handled, remember to call "done()" after completion. If you are returning a Promise, make sure that it resolves properly for puppeteer and

I am currently testing my component using mocha and Google Puppeteer. In my unit test file, I have set up the Puppeteer browser to launch before the tests and close after the tests in the respective functions. However, when running the test file, I encou ...

Comparing the efficiency of AngularJS in generating DOM elements: traditional JavaScript versus jqLite

While working on an AngularJS directive that will display a table with over 1000 rows, my boss advised against using binding due to Angular's limit of around 2000 bindings. Instead, I created the DOM elements dynamically using angular.element(...), wh ...

How to troubleshoot incorrect div width detection by jQuery in Bootstrap

My asp.net mvc4 project utilizes bootstrap for markup and jquery for client side functionality. <div class="row"> <div class="col-md-4 container" id="cont"> <div class="row overlay_container" id="overlayCont"> <div class=" ...

Transfering information to handlebars in node.js

Being a beginner in node.js, I am currently working on making a get request in my router (index.js). After successfully obtaining the desired result (verified by logging it in console.log), I proceed to parse it into a JSON object and pass it to a render f ...

Obtaining a collection of distinct values from an observable stream or pipeline

I'm stuck on figuring out how to filter for distinct values while filtering within a .pipe() <mat-form-field class="filterField"> <input formControlName="filterParam2" matInput placeholder="Filter& ...

Ways to transfer data from a single Ajax request to another

I'm currently working on a piece of code that involves making two Ajax requests to retrieve data from a PHP class. { xtype: 'button', formBind: true, id: 'saveLicenseBtn', text: 'Save', listeners: { ...

Ways to determine the text field value without the need for a submit button

I need help with a calculation that doesn't require a submit button. I want to automatically calculate and display the total value in a text field based on the weight, height, and number of pieces entered by the user without having to refresh the page ...

Using jQuery to trigger an event when an element is empty or when the element contains children nodes

Is there a way to create a jQuery script that can monitor the children of an element? If the element does not have any children, default scrolling for the entire page is enabled. If the element has children, scrolling for the whole page is disabled. The ...

Exploring jQuery's AJAX capabilities in handling cross-domain requests and implementing Basic Authentication

I attempted the solutions provided in the suggested duplicate question, but unfortunately, they did not yield the desired outcome. I have been struggling to utilize AJAX to POST data to a remote server's API from a local PC client (testing on Chrome ...

Splitting the webpage into two sections with individual scroll bars

My website consists of two elements, a left and right part, both taking up 50% of the screen. <div class="wrapper"> <div class="left"> <div class="box"></div> </div> ...

Is there a way to adjust the pivot point of a cone scale in three.js?

I am trying to resize a cone that is generated using THREE.CylinderGeometry. My goal is to make it longer and have it grow in the direction of the negative z-axis, but I've noticed that currently it's growing in both negative and positive directi ...

The Typescript Decorator is triggered two times

I submitted a bug report regarding Typescript because I suspect there is an issue, although I'm seeking additional insights here as well. This is the scenario. When running the following code: class Person { @IsValueIn(['PETER', ' ...

A guide on utilizing Material UI Fade for smoothly fading in a component when a text field is selected

I am facing an issue with a text field input and a helper component. My goal is to have the helper component fade in when a user focuses on the input field. The helper component is wrapped as follows: <Fade in={checked}> <DynamicHelperText lev ...