2017-02-16 23 views
1

Я хочу получить доступ к моему основному приложению приложения от главного контроллера для визуализации нового представления. Что я хочу сделать, это комментировал в контроллер/main.js кода, как я понимаю, что это зависимость CIRC в требуют JS, но я не понимаю, как это исправитьMarionette js доступ к приложению от контроллера с requirejs

file structure 
     - controllers/main.js 
     - models/ 
     - templates/ 
     - views/ 
     - app.js 
     - main.js 
     - router.js 

main.js

require.config({...}) 
require(['app'], function(app) { 
    app.initialize().start(); 
}); 

app.js

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

    let app = null; 

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

    return { 
     get instance() { 
      return app; 
     }, 
     initialize() { 

      if (!app) { 
       const controller = new MainController(); 
       const router = new Router({controller}); 
       app = new App({router}); 
      } 
      return this; 
     }, 
     start() { 
      this.instance.start(); 
     }, 
    } 
}); 

router.js

define([ 
    'jquery', 
    'underscore', 
    'marionette', 
], function($, _, Mn) { 
    return Mn.AppRouter.extend({ 
     initialize(options) { 
      this.controller = options.controller; 
     }, 
     appRoutes: { 
      '': 'index', 
      'profile': 'profile', 
      '*notFound': 'notFound' 
     }, 
    }); 
}); 

контроллеры/main.js

define(['jquery', 'underscore', 'marionette', 'app'], function($, _, Mn, app) { 
    return Mn.Object.extend({ 
     index() { 
      console.log(app); // undefined 
      console.log('index method invoked'); 
      /* 
      i want to do like this 
      app.showView(new SomeView()); 
      or this.triggerMethod('render', 'someView') and then listen for this event from app.js like this.router.controller.on('render', handler) and dynamic require somehow... 
      or what is best practice ? iam confused 
      */ 
     }, 
     profile() { 
      console.log('profile method invoked'); 
     }, 
     notFound() { 
      console.log('notFound method invoked'); 
     } 
    }); 
}); 
+0

Почему просто не дать экземпляр для вашего конструктора 'controller/main.js'? – gpgekko

+0

извините? можете ли вы привести пример? –

+0

Что-то вроде 'const controller = new MainController (instance());' – gpgekko

ответ

1

Вы можете асинхронно загружать приложение внутри index метода контроллера (там, где это необходимо), а не добавлять его в качестве зависимости модуля

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'); 
    } 
    }); 
});