gpt4 book ai didi

javascript - RequireJS 2.0 - 定义我的 Backbone.Router

转载 作者:行者123 更新时间:2023-11-30 13:17:01 25 4
gpt4 key购买 nike

我目前正在尝试 RequireJS 2.0,因此必须更改一些代码。这是我的 main.js,在我的 index.phtml 中使用 data-main 启动。

编辑:忘记了项目结构,很有用:)

->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 时加载的 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;
});

最佳答案

您的 router.js 文件没有正确定义 AMD 模块。您需要使用 define,而不是 require

关于javascript - RequireJS 2.0 - 定义我的 Backbone.Router,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11688052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com