我在 RequireJS/Backbone.js 应用程序中遇到 HTML5 pushState 的问题,我很确定我做错了什么,但我无法确定问题,我已经尝试了几个小时。
前言:所有 RequireJS 依赖项都在其正确的文件夹中。
这是我的问题:我有一个基本的小设置——我使用的唯一主干组件是路由器。在默认的 '' 路由上,我在路由器中调用了一个 'home' 方法。这种方法只不过是提醒“测试”而已,它确实有效。
但是,一旦我将{pushState: true}
参数添加到Backbone.history.start()
顶级app.js
文件中,就不再调用“home”方法。
这些是发生它的代码块:
索引.html:
<!doctype html>
<html>
<head>
<title>Todo</title>
<script data-main="assets/js/app/app.js" src="assets/js/app/lib/require.js"></script>
</head>
<body>
<div id="main"></div>
</body>
</html>
应用程序.js:
require.config({
baseUrl: 'assets/js/app',
paths: {
'underscore': 'lib/underscore',
'jquery': 'lib/jquery',
'backbone': 'lib/backbone',
'text': 'lib/text',
'handlebars': 'lib/handlebars',
'router': 'router/router'
},
shim: {
'underscore': {
exports: '_'
},
'backbone': {
deps: ['underscore', 'jquery'],
exports: 'Backbone'
}
}
});
require(['router', 'backbone'], function(Router, Backbone) {
var router = new Router();
Backbone.history.start({ pushState: true });
});
路由器.js:
define(['backbone'], function(Backbone) {
var Router = Backbone.Router.extend({
routes: {
'': 'home'
},
home: function() {
alert('test');
}
});
return Router;
});
我究竟做错了什么?这是一种不正确、复杂的方法吗?