2012-07-27 37 views
1

我目前正在嘗試RequireJS 2.0,因此必須更改一些代碼。 這裏是我的main.js,與data-main在我的index.phtml推出。RequireJS 2.0 - 定義我的Backbone.Router

編輯:忘記項目結構,可能是有用的:)

->public 

-> css 
-> js 
app.js 
facade.js 
main.js 
router.js 
... 
    -> collections 
    -> libs 

    -> backbone 
    -> jquery 
    -> json2 
    -> plugins 
    -> require 
    -> underscore 

-> templates 
... 


// Filename: main.js 

// Require.js allows us to configure shortcut alias 
// There usage will become more apparent futher along in the tutorial. 

require.config({ 
    //Here baseUrl = /js (Loaded in data-main in logged.phtml) 
    // 3rd party script alias names (Easier to type "jquery" than "libs/jquery-1.7.2.min") 
    paths: { 
     jQuery: 'libs/jquery/jquery', 
     Underscore: 'libs/underscore/underscore', 
     Backbone: 'libs/backbone/backbone', 
     JSON: 'libs/json2/json2', 
     templates: '../templates' 
    }, 

    // Sets the configuration for your third party scripts that are not AMD compatible 
    shim: { 
     'libs/plugins/bootstrap-min': ['jQuery'], 
     'libs/plugins/jquery.cookies.min': ['jQuery'], 
     'jQuery': { 
      exports: '$' 
     }, 
     'JSON': { 
      exports: 'JSON' 
     }, 
     'Underscore': { 
      exports: '_' 
     }, 
     'Backbone': { 
      deps: ['Underscore', 'jQuery'], 
      exports: 'Backbone' // attaches "Backbone" to the window object 
     } 
    } 
}); 

define([ 
    // Load our app module and pass it to our definition function 
    'app', 
    'jQuery', 
    'Underscore', 
    'Backbone', 
    'JSON' 
], function(App) { 
    // The "app" dependency is passed in as "App" 
    // Again, the other dependencies passed in are not "AMD" therefore don't pass a parameter to this function 
    App.initialize(); 
}); 

我試圖「路由器」添加到該文件與它的依賴,但一切都沒有改變,所以我認爲這是沒有用的。

而這裏的調用應用程序時加載的app.js:

define([ 
    'jQuery', 
    'Underscore', 
    'Backbone', 
    'JSON', 
    'router', // Request router.js 
    'i18n!nls/langs', 
    'facade', 
    //'order!libs/plugins/jquery.backstretch.min' 
    //'libs/plugins/backbone.validation.min' 
], function($, _, Backbone, JSON, Router, langs, facade) { 
var initialize = ... 
... 
... 
return { 
     initialize: initialize 
    }; 
}); 

這裏的問題,在我的router.js定義,我用了一個console.log,它表明我,這是返回正確的對象。

但是,當我在我的app.js中撥打router時,router保持未定義狀態。 我可能犯了一個錯誤,但我找不到它。

編輯:添加了router.js代碼

require([ 
    'jQuery', 
    'Underscore', 
    'Backbone', 
    'JSON', 
    'facade', 
    'models/UserModel', 
    'libs/plugins/jquery.cookies.min' 
], function($, _, Backbone, JSON, facade, User, cookies){ 
    var AppRouter = Backbone.Router.extend({ 
... 
    }); 

    var app_router = new AppRouter; 
     // Start Backbone history a neccesary step for bookmarkable URL's 
     Backbone.history.start(); 
    return app_router; 
}); 
+0

router.js是否正確定義AMD模塊? – EndangeredMassa 2012-07-29 07:38:58

+0

嗯,你說的讓我想到了一點,我檢查了我的'router.js',有一個'require'而不是'define',所以它不會返回給我一個AMD模塊...做出這樣的回答我可以接受它;)!謝謝 (我用'router.js'編輯+原始錯誤,所以你可以指出) – Sky 2012-07-30 09:29:10

回答

3

router.js文件沒有正確定義AMD模塊。您需要使用define,而不是require

+0

我浪費了我的2個小時,只是因爲這個原因......謝謝你的回答是直截了當的! +1 – Shubh 2013-11-28 12:39:48