Skip to content Skip to sidebar Skip to footer

Marionette Js Access To App From Controller With Requirejs

I want to get access to my main app instance from main controller to render new View. What i want to do is commented in controller/main.js code, as i understand this is circ depend

Solution 1:

You can asynchronously load the app inside index method of controller (wherever you need it) rather than adding it as a dependency of module

define(['jquery', 'underscore', 'backbone', 'marionette', 'router','controllers/main'],
  function($, _, Backbone, Mn, Router, MainController) {

    const App = Mn.Application.extend({
      region: '#app',
      initialize(options) {
        this.router = options.router;
      },
      onBeforeStart() {
        console.log('before start');
      },
      onStart() {
        Backbone.history.start();
      }
    });

    const controller = new MainController();
    const router = new Router({
      controller
    });

    return app = new App({
      router
    });
  });

define(['jquery', 'underscore', 'marionette'], function($, _, Mn) {
  return Mn.Object.extend({
    index() {
      require(['app'],function(app){
        console.log(app);
      });
    },
    profile() {
      console.log('profile method invoked');
    },
    notFound() {
      console.log('notFound method invoked');
    }
  });
});

Post a Comment for "Marionette Js Access To App From Controller With Requirejs"