1

我正在使用backbone.js 构建一个单页站点。我正在使用 require.js 使代码模块化,但在为 url 初始化骨干路由器时遇到问题。我使用 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:无法读取属性 'Router' of null”。我究竟做错了什么?

4

1 回答 1

0
define(['backbone','routers/workspace'],
    function( Backbone,Workspace) {
        $(function () {
            var space = new Workspace();
        } );
    } );

是单独的文件吗?也许你需要加载 jQuery 才能使用 $?

于 2012-05-30T11:48:58.943 回答