Problem with Marionette AppRouter compatibility when used with Browserify

app.js

module.exports = function () {
if (!window.__medicineManager) {
    var Backbone =  require('backbone'); 
    var Marionette = require('backbone.marionette');

    var MedicineManager = new Marionette.Application();

    MedicineManager.addRegions({
        mainRegion: "#main-region"
    });

    MedicineManager.navigate = function (route, options) {
        options || (options = {});
        Backbone.history.navigate(route, options);
    };

    MedicineManager.getCurrentRoute = function () {
        return Backbone.history.fragment;
    };

    MedicineManager.on("start", function () {
        if (Backbone.history) {
            Backbone.history.start();
        }
    });

    MedicineManager.start();
    window.__medicineManager = MedicineManager;
}
return window.__medicineManager;
}();

medicine_app.js

var MedicineManager = require('app');
var Backbone = require('backbone');
var Marionette = require('backbone.marionette');
MedicineManager.module("MedicineApp", function (MedicineApp) {
    MedicineApp.Router = Marionette.AppRouter.extend({
        appRoutes: {
            "": "showSearchOption",
            "show/*id": "showMedicine"
        }
    });

    var API = {
        showSearchOption: function () {
            console.log('show search option');
        },
        showMedicine: function (id) {
            console.log('show ' + id);
        }
    };

    MedicineManager.addInitializer(function () {
        console.log('MedicineApp.Router initialised');
        new MedicineApp.Router({
            controller: API
        });
    });
});

I am currently utilizing Browserify for testing purposes, so please disregard the fact that Marionette module is deprecated.

The current issue revolves around the "": "showSearchOption" route not being triggered.

This problem has been mentioned in the following link: Marionette.AppRouter as Backbone.Router and I am seeking guidance on how to resolve it.


Edit:
Initially, I believe that the internal Backbone instance utilized by Marionette differs from what is obtained when using require('backbone').
Any suggestions to tackle this issue?

Answer №1

It appears that the main issue lies in Marionette's requirement for its own instance of Backbone.

After struggling with this problem for some time, I finally found a solution that worked for me. By upgrading to NPM v3 with a flat dependency structure and then utilizing

window.Backbone = require( 'backbone' );
, I was able to resolve the issue.

Alternatively, if you prefer not to take this approach, you may also consider using peerDependencies. You can find more information on this method here. Please note that I have not personally tested this method as I am satisfied with the results from using NPM v3.

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 mousedown, mousemove, and mouseup to handle touch events in vanilla JavaScript instead of jQuery

Can someone guide me on how to set up a touch event handler in JavaScript using the mousedown, mousemove, and mouseup events? I would really appreciate any suggestions or tips! ...

Sending template reference from one Angular component to another

I have a main grid component that includes smaller grid-item components. The majority of these grid items navigate to a specific route when clicked. However, there is one particular item that should open a modal window instead of navigating. Is there a wa ...

issue with retrieving data from PHP script via ajax

My attempts to use AJAX to call a PHP script have been unsuccessful. I added an echo alert statement in my deleteitem.php script to ensure it was being called, but no matter what I tried, it never executed. Both the PHP script and the JS script calling it ...

Npm publish seems to be stuck without generating any output

I'm having trouble trying to publish a package that I created. The files can be found at this Github Repo. When I run the command npm publish, it just seems to hang without any errors or output. I've attempted logging in using npm login and npm a ...

Failed to complete the installation of cordova

Upon executing the following command: sudo npm install -g cordova An error message is returned as follows: npm ERR! notarget No compatible version found: JSONStream@'>=1.0.3-0 <2.0.0-0' npm ERR! notarget Valid install targets: npm ERR! n ...

Difficulty encountered while implementing mouseover event listener for an SVG component

I'm having trouble triggering an event for an svg element. Here's the link to my jsfiddle example: https://jsfiddle.net/r1ahq5sa/ Here's the HTML code: <div class="row"> <div class="col-md-8"> <svg class="video-nav-bar ...

The execution of the command 'npm' was completed with an unexpected exit code 254

Issue with Building Project While trying to build my project, I encountered the following error: Error message - Execution failed for task ':yarnSetup'. Process 'command 'npm'' finished with non-zero exit value 254 Error S ...

Learn how to mock asynchronous calls in JavaScript unit testing using Jest

I recently transitioned from Java to TypeScript and am trying to find the equivalent of java junit(Mockito) in TypeScript. In junit, we can define the behavior of dependencies and return responses based on test case demands. Is there a similar way to do t ...

Encountering an issue with the message "SyntaxError: Unexpected token < in django-jquery-file

I am currently working on implementing django-jquery-fileupload into my project. https://github.com/sigurdga/django-jquery-file-upload However, I encounter an "Error SyntaxError: Unexpected token < " when attempting to click the "start" upload button. ...

When faced with the error message "Typescript property does not exist on union type" it becomes difficult to assess the variable

This question is a continuation of the previous discussion on Typescript property does not exist on union type. One solution suggested was to utilize the in operator to evaluate objects within the union. Here's an example: type Obj1 = { message: stri ...

Search for a specific folder and retrieve its file path

Is there a way for me to include an export button on my webpage that will allow users to save a file directly to their computer? How can I prompt the user to choose where they want to save the file? The button should open an explore/browse window so the ...

The ngModel directive seems to be failing to bind the select element in Angular 4

One of the challenges I am facing in my application involves a form that includes various data fields such as title, price, category (select), and imageUrl. I have successfully implemented ngModel for all fields except the select element. Strangely, when I ...

Adding a cell break line within AG-GRID in an Angular application

I'm trying to display two sets of data in a single cell with ag-grid, but I want the data to be on separate lines like this instead: Data with break line I attempted to use "\n", "\r", and "\br" but it didn't work. Here is my code ...

Can you help identify the issue in this particular ajax code?

Here is the code I wrote to check if a username exists in the database using Ajax. However, I am facing an issue where the input text from the HTML page is not being sent to the checkusername.php file via $_POST['uname'];. I have tried multiple s ...

Creating new components within A-frame

I've been attempting to implement the bubble function into my scene to generate a sphere, but unfortunately nothing is showing up. Even when I try creating a sphere without using the bubble function, it still doesn't appear in the scene. func ...

Is it possible to host multiple React applications on a single port? Currently experiencing issues with running both an Admin panel and the Front side in production mode on the same Node.js API server

Is it possible to host multiple React applications on the same port? I am experiencing issues with running both an Admin panel and a Front side React app in production mode on the same Node.js API server. ...

The value of element.scrollTop is consistently zero

Why does the scrollTop position of an element always return 0? How can I correct this issue? JSFiddle var inner = document.getElementById('inner'); window.addEventListener('scroll', function() { console.log(inner.scrollTop); }) # ...

Angular 1.5 - Component for fetching HTML content with dynamic data

Help needed with using Angular Component method. Developing a main html file with its main controller containing a JSON list of client data: clients: [{ "name": "John Jackson", "age": "21", "hair": "brown", }, { "name": "Janet Doe", ...

What is npm's reason for attempting to install PhantomJS?

Having an issue: I recently set up a new Linux distro on my pi (raspian) and was attempting to install a list of packages with one npm command: npm install telegraf blocktrail-sdk http request cookie-parser express-ipfilter googleapis express body-parse ...

Having difficulty executing the Cypress open command within a Next.js project that uses Typescript

I'm having trouble running cypress open in my Next.js project with Typescript. When I run the command, I encounter the following issues: % npm run cypress:open > [email protected] cypress:open > cypress open DevTools listening on ws: ...