2012-10-03 37 views
0

我的所有代碼都被找到並加載,但我的應用程序是mapApp.js文件從未找到,並且在嘗試使用它時總是給我一個未定義的代碼。要求JS找不到我的應用程序

我在做什麼錯?

這是我的文件夾層次

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

回答

1

您必須返回應用功能:

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

在這種情況下,骨幹,下劃線和jQuery在頁面全局變量。我認爲這是有道理的,因爲你總是需要它們。

+0

哼,你釘了它!我是否必須返回在所有類中存儲我的Backbone對象的var?像模型和收藏呢?我不喜歡那樣買像我一樣:( –

+0

要求已經非常痛苦的IDE,特別是當你已經買了IntelliJ的150美元許可:)我不認爲你需要添加更多的混亂所有這些變量。 –

相關問題