2017-02-16 91 views
1

我想从主控制器 访问我的主应用程序实例以呈现新的View。我想要做的控制器/ main.js代码注释,按照我的理解,这是在要求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

为什么不干脆放弃实例到你的'controllers/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'); 
    } 
    }); 
}); 
相关问题