骨干木偶:Marionette.Application造成Require.js模块加载错误,"'错误:模块名称'应用'尚未加载方面:_" [英] Backbone Marionette: Marionette.Application causing Require.js module load error, "'Error: Module name 'App' has not been loaded yet for context: _"

查看:189
本文介绍了骨干木偶:Marionette.Application造成Require.js模块加载错误,"'错误:模块名称'应用'尚未加载方面:_"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想包括应用程序实例来使用它的事件聚合如图所示<一个href=\"https://github.com/derickbailey/backbone.marionette/blob/master/docs/marionette.application.md\"相对=nofollow>这里

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.

在Requirejs配置文件踢东西了,从App.Bootloader.js:

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

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

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

从Layout.js:

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

   });

});

从/folder/folder/View.js:

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.

推荐答案

我也在寻找来处理与这种利用木偶和require.js情况的好办法

I'm also looking for a good way to handle with this kind of situations using marionette and require.js

我在一个解决方案的工作,但我不知道这是否是最好的方法,这里有我的想法:

I have worked on a solution but I don't know if it is the best way, here are my thoughts:


  • 的应用程序连接到事件的操作,无需了解的意见

  • 我们使用触发器附加动作事件视图内

  • 的视图和应用程序之间的行动连接视图中所做

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

This is a possible solution:

app.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;
});

view.js

view.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
}); 

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

it is important to remember that require is asynchronous, so when we use it like this it will not be executed immediately:

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

我们可以prepare对象至极点到外部环境,像这样的:

we can prepare objects wich point to the external context, like this:

var thisName = this;
require( ['some-module'], function( someModule ){ 
   // access to external this using thisName
});

这篇关于骨干木偶:Marionette.Application造成Require.js模块加载错误,&QUOT;'错误:模块名称'应用'尚未加载方面:_&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆