0

すべてのdepsが検出されて読み込まれていますが、mapApp.jsファイルであるアプリケーションが検出されず、使用しようとすると常にUndefinedが表示されます。

私は何を間違っているのですか?

これは私のフォルダ階層です

Site
  |
  |- JS 
      |- Libs
      |    |- * All my deps *
      |
      |- mapApp.JS
      |
      .
      .
      .
      |- /models
      |- /views
      |- /collections

これはrequire.jsを初期化する私のMain.jsファイルです

require.config({
  baseUrl: '/ctt-ct/js/'
  ,urlArgs: "ts=" +  (new Date()).getTime()

  ,paths: {
      'jquery': 'libs/jquery.min'
      ,'underscore': 'libs/underscore-min'
      ,'backbone': 'libs/backbone'
      ,'templates': '../templates'
  }

  ,shim: {
    jquery: {
      exports: '$'
    }
    ,underscore: {
      exports: '_'
    }
    ,backbone: {
        deps: ['underscore', 'jquery'],
        exports: 'Backbone'
    }
  }
});

require([
  'jquery'
  ,'underscore'
  ,'backbone'
  ,'mapApp'
],
function ($, _, Backbone, App) {
  $;                       // <- working
  _;                       // <- working
  Backbone.View;           // <- working
  var app = new App();     // <- NOT working !!!
});

mapApp.js

require([
  'jquery'
  ,'underscore'
  ,'backbone'
],
function ($, _, Backbone) {
    var App = Backbone.View.extend({

        el : $('#map_canvas')    
        ,initialize : function(){
                 // DO a lot of stuff and don't return anything.
        }

        ,drawData: function(){
                 // Do other stuff. 
        }
    });
});
4

1 に答える 1

1

関数からAppを返す必要があります:

...
function ($, _, Backbone) {
    var App = Backbone.View.extend({

    });

    return App;
});

通常、私はそのように必要なものを使用しませんが、正しい方法がまったくわかりません(ドキュメントはあまり親しみがありません)。私はよく書くでしょう:

mapApp.js

define([
  'views/otherView' //Other BackboneView
],
function (OtherView) {
    var App = Backbone.View.extend({

        el : $('#map_canvas')    
        ,initialize : function(){
            // stuff ; not too much in a View
        }

        ,render : function(){
             var otherView =  new OtherView();
             ...
             return this;
        }
    });
    return App;
});

その場合、Backbone、Underscore、およびjQueryはページ内のグローバル変数です。あなたがいつもそれらを必要とするので、それは理にかなっていると思います。

于 2012-10-03T18:25:47.103 に答える