0

I'm trying to include the App instance to use it's event aggregator as shown here

I get an error when I include the instance in a view.

Kicking things off in the Requirejs config file, from App.Bootloader.js:

require(['App'], function (App){
      App.start();
      });

from App.js:

define(function (require){

  //...requisite includes $, _, Backbone, Marionette ...

var Layout = require('Layout');

  var App = new Marionette.Application();

        App.addRegions({
            main: '#view_content'
        });

        App.addInitializer(function (){

                App.main.show(new Layout());
                //... adding router etc ...    

                Backbone.Marionette.TemplateCache.loadTemplate = function (template, callback){
                   callback.call(this, Handlebars.compile(template));
                };
                Backbone.history.start();

        });

    return App;
});

From Layout.js:

define(function(require){
   var View = require('folder/folder/View');
   //template contains #sub div
   var template = require('text!template.html');

   return Marionette.Layout.extend({
      template: template,
      regions: {
         sub: '#sub'
      },
      initialize: function(){
         //wait till template is rendered in dom
         _.defer(function(region){
             region.sub.show(new View());
          }, this)
      }

   });

});

From /folder/folder/View.js:

define(function (require){

      //...requisite includes $, _, Backbone, Marionette ...

     var App = require('App');
     return Marionette.ItemView.extend({});
});

Where I get the error "'Error: Module name 'App' has not been loaded yet for context: _"

Any ideas? Lemme know if you need more information.

4

4 回答 4

5

我也在寻找一种使用 marionette 和 require.js 处理这种情况的好方法

我已经研究了一个解决方案,但我不知道这是否是最好的方法,这是我的想法:

  • 应用程序在不了解视图的情况下将操作附加到事件
  • 在视图中,我们使用触发器将动作附加到事件
  • 视图和应用程序之间的动作连接在视图内部进行

这是一个可能的解决方案:

应用程序.js

define( [ 'underscore', 'jquery', 'backbone', 'marionette' , 'view'], 
    function( _, $, Backbone, Marionette, View  ) {

    var app = new Marionette.Application();
    app.addRegions({ content: '#content'})

    app.on( "initialize:after", function(){

        console.log( 'after init', app)

        var view = new View();
        app.content.show( view );

    });

    // this is the action that we would like to call from the view      
    app.vent.on( 'viewClick', function(){ console.log( 'clicked on view' )})

    return app;
});

视图.js

define( [ 'underscore', 'jquery', 'backbone', 'marionette' ], 
    function( _, $, Backbone, Marionette ) {

    var View = Marionette.ItemView.extend({
        template: '#view',

        triggers: {
            'click': 'clicked'
        },

        initialize: function(){

            // thisView will be referring to the view instance
            var thisView = this;

            // we require the app because we need access to the event aggregator 
            require(['app'], function( app ){

                // when our event is triggered on our view
                thisView.on( 'clicked', function(){ 
                    // we trigger an event on the application event aggregator
                    app.vent.trigger( 'viewClick' )
                });
            });
       }
    })

    return View
}); 

重要的是要记住 require 是异步的,所以当我们这样使用它时,它不会立即执行:

require( ['some-module'], function( someModule ){ 
   // do something, but only as soon as someModule is loaded 
});

我们可以准备指向外部上下文的对象,如下所示:

var thisName = this;
require( ['some-module'], function( someModule ){ 
   // access to external this using thisName
});
于 2012-07-07T02:04:09.093 回答
3

我猜你有循环依赖的问题。App需要ViewView需要App。嗯……但为什么View需要App?我无法从您的代码中计算出来。最后,你确定View需要App吗?顺便说一句,我认为你打错了。第一个From /folder/folder/View.js大概应该是From Layout.js

于 2012-06-16T00:25:49.930 回答
0

user1248256 是正确的。我有同样的问题。我的应用需要一个控制器,而我的控制器需要应用。

通过将控制器(代码视图)作为选项的一部分传递,我不必将其添加到 require.js 定义中。

//data-main:
define(function(require) {
    var   $                 = require("jquery"),
        _                   = require("underscore"),
        App                 = require("app/App"),
        PublicRouter        = require("routers/DesktopRouter"),
        PublicController    = require("routers/publicController");

    var options = {
        publicController  :   PublicController,
        publicRouter      :   PublicRouter

    }

    App.start(options);
});

现在在应用程序中,我不必“要求” PublicController

//App:
define(['jquery', 'backbone', 'marionette', 'underscore'],
function ($, Backbone, Marionette, _) {
    var App = new Marionette.Application();
    ...snip...
        console.log("Creating Routers");
        App.Routers = {};
        // Connect controllers to its router via options
        // init router's router/controller
        App.Routers.publicRouter = new options.publicRouter.Router({
            controller: options.publicController
        });
    });

希望有帮助。

安德鲁

于 2013-06-05T15:48:53.657 回答
0

我通常认为在使用 requireJS 时使用应用程序的 EventAggregator 是一种不好的做法,如果没有其他原因,只是很容易以循环引用结束。

只需定义一个单独的 EventAggregator 模块,App、View 和 Layout 都可能需要它,然后将其添加到任何需要它的模块的依赖项中。

于 2014-08-21T20:17:14.967 回答