RequireJS 2.0-定义我的Backbone.Return
RequireJS 2.0 - Defining my Backbone.Router
我目前正在尝试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();
});
我试图将"router"及其依赖项添加到这个文件中,但没有任何更改,所以我认为它毫无用处。
以下是调用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
。
相关文章:
- backbone.js无法渲染视图
- 如何使用backbone.js从集合中获取模型名称
- Backbone LayoutManager渲染问题
- Backbone.js restful json API design
- Backbone虹吸以获取对象数组
- 2个backbone.js集合,具有相同的模型,但排序顺序不同
- jquery if语句返回return wong语句
- Backbone.js micro templating
- 函数在return语句之前返回空对象
- Javascript Return and if/else
- FancyTree JS+Backbone.JS-将数据加载到FancyTree时出现问题
- Backbone.js将模型绑定到视图时出错
- minimy javascript是什么意思?Backbone.js
- 在ES6 Promise中,我应该在解决/拒绝之前使用return吗
- 点击功能没有'ajax调用动态元素(Backbone)后无法工作
- Backbone.js视图中的多个模型
- 如何使用Backbone.Return捕获查询参数的更改
- RequireJS 2.0-定义我的Backbone.Return
- Backbone.model:对象函数 (a){return new n(a)} 没有方法 'has'
- Backbone.js-模型中的集合-对象函数(a){return new n(a)}没有方法'有'