2012-06-29 39 views
0

我已經配置了requirejs來加載核心庫(jQuery,下劃線,主幹)。 現在我想添加我的骨幹模型,控制器,視圖等異步加載Requirejs2:如何處理自己的文件?

我發現了很多教程這個主題和很多「準備好」樣板不幸我提到,大多數方法是depreceated或相當複雜(即使有更好的方法)。

一個例子是我如何配置的主庫requirejs: https://stackoverflow.com/a/10914666/1309847

因此,我怎麼加載骨幹意見,型號,類別,路由器,控制器和模板,用一簡單和有效的Requirejs配置?

我跟着您選擇的建議,但得到一些奇怪的錯誤

main.js

require.config({ 
    paths: { 
     jquery: 'vendors/jquery/jquery', 
     underscore: 'vendors/underscore/underscore', 
     backbone: 'vendors/backbone/backbone' 
    }, 
    shim: { 
     underscore: { 
      exports: '_' 
     }, 
     backbone: { 
      deps: ['underscore', 'jquery'], 
      exports: 'Backbone' 
     } 
    } 
}); 

require(['app'], function(app){ 

}); 

app.js

define(['jquery', 'underscore', 'backbone'], function($, _, Backbone){ 
    var Message = new Backbone.Model.extend({ 
     //idAttribute: '_id', 
     //defaults: { body: '' } 
     //url: function(){ return this.id ? '/messages/' + this.id : '/messages'; } 
    }); 

    var newMessage = new Message({ body: 'hi' }); 
    newMessage.save(); 
}); 

在app.js錯誤occours:

Uncaught TypeError: Object [object Object] has no method 'apply' 

當我評論新的Backbone.Model.extend部分時,我不再有任何錯誤。

+1

似乎加載下劃線或主幹網時出現問題。在app.js的第二行放置一個斷點,並檢查「_」或「Backbone」是否未定義。在這種情況下,請檢查您的路徑,以及libs版本尋求兼容性問題。如果一切都失敗了,請嘗試使用AMD骨幹版本和undercore:https://github.com/amdjs – eabait

+0

我做了一個console.log三個參數。所有三個看起來有效,但我已經指定了更多的錯誤。它發生在我想創建一個新的Message實例時。 (我也可以取消註釋模式,但實例化會給出錯誤) –

+1

好的,刪除此行中的「new」「var Message = new Backbone.Model.extend」。它應該是var Message = Backbone.Model.extend – eabait

回答

1

根據我的經驗,引導您的應用程序的最佳方式是創建一個Backbone.Router。因此,您可以將網址與您的應用程序功能相關聯。 如果你使用的是RequireJS + Backbone,你可能有一個main.js配置RequireJS(路徑,墊片等)。第一次調用「require」用於加載初始腳本以引導整個應用程序。 例如:

/** 
* main.js - RequireJS bootstrap 
*/ 
require.config({ 
    paths: { 
    //your paths 
    }, 
    shim: { 
    //your shims 
    } 
}); 

require(
    [ 
    'app' //app.js is at the same directory as main.js 
    ], 
    function(app) { 
    app.init(); 
    } 
); 

然後在app.js你可以創建一個新的路由器的實例,或者你可以開始創建視圖和模型。

有待進一步參考:http://addyosmani.github.com/backbone-fundamentals/

+0

謝謝你的例子!在學習骨幹網時,我試圖首先遵循你的建議,使用app.js和視圖,模型,集合等。但是這給了一些奇怪的錯誤。我編輯了開始線程 –

0

因此,我現在明白了:你必須在你自己的自定義js文件中包裝一個requirejs函數。

該函數被稱爲define。第一個參數是您在main.js文件中定義的依賴關係的數組,或者是您的另一個自定義js的相對路徑。 第二個參數是保存原始文件的回調。重要的是你返回你想要共享的對象,函數,數組或變量。

整個事情是這樣的:

define(
    ['underscore', 'backbone'], // the dependencies (either relative paths or shortcuts defined in main.js 
    function(_, Backbone){ // the return statement of the deps mapped to a var 
     var MessageModel = Backbone.Model.extend({ // the original code, file 
      defaults: { body: '' }, 

      initialize: function(){} 
     }); 

     return MessageModel; // the return statement, sharing the "final result", sometimes you return the initialize parameter 
    }); 

同爲一家集包裝型號:

define(
    ['jquery', 'underscore', 'backbone', 'models/message_model'], // deps and the last one is the relative path 
    function($, _, Backbone,MessageModel){ // same as above explained 
var MessageCollection = Backbone.Collection.extend({ 
    model: MessageModel, 

    initialize: function(){} 
}); 

return MessageCollection; 

});

我現在只需要弄清楚我如何可以引導到整個應用程序。但我認爲我需要更多的骨幹知識來做到這一點:)

相關問題