使用backbone.js构建单页网站。我使用require.js来使代码模块化, 但是我在为网址初始化骨干路由器时遇到了问题。 我使用require.js加载主js文件,看看它是什么样的
enter code here
//App Namespace
var Chrono = Chrono || {};
//App Config Namespace
Chrono.Config = Chrono.Config || {};
//App Views Namespace
Chrono.Views = Chrono.Views || {};
//Config
Chrono.Config = {
url:"http://localhost/chronotech/",
site_url:"http://localhost/chronotech/index.php/",
data_source:"http://localhost/chronotech/assets/datasource/"
};
require.config({
paths : {
'backbone': 'libs/backbone',
'jquery':'libs/jquery.min',
'underscore':'libs/underscore',
'text':'libs/require/text'
},
baseUrl : 'assets/js'
});
require(
['require', 'underscore', 'backbone', 'jquery'],
function(require,_, Backbone, $) {
require(['app'],
function(require) {
} );
} );
app.js看起来像这样
define(['backbone','routers/workspace'],
function( Backbone,Workspace) {
$(function () {
var space = new Workspace();
} );
} );
路由器文件看起来像这样
define(['jquery','backbone'],
function($, Backbone) {
var Workspace = Backbone.Router.extend( {
routes: {
"about":"aboutPage",
"team":"teamPage",
"contact":"contactPage",
"work":"portfolioPage",
"products":"productPage"
},
aboutPage : function() {
alert("about");
},
teamPage : function() {
alert("team");
},
contactPage: function() {
alert("contact");
},
portfolioPage : function() {
alert("work");
},
productPage : function() {
}
} );
return Workspace;
} );
当我加载页面时,我收到以下错误: "未捕获的ReferenceError:$未定义", "未捕获的TypeError:无法读取属性'路由器' of null"。 我做错了什么?
答案 0 :(得分:0)
define(['backbone','routers/workspace'],
function( Backbone,Workspace) {
$(function () {
var space = new Workspace();
} );
} );
这是一个单独的文件吗?也许你需要加载jQuery才能使用$?