6

我一直从 Backbone 开始。我使用 Require 加载模块并收到此错误。谁能解释一下?在文件 user.js 中,当我尝试创建 Backbone.Model.extend 时,我遇到了这个错误。我使用了 Backbone.js 0.9.10、jQuery v1.9.0、RequireJS 2.1.4、Underscore 1.4.4。谢谢你。

索引.html

<!doctype html>
<html lang="en">
<head>
    <title></title>
    <!-- Load the script "js/main.js" as our entry point -->
    <script data-main="js/main.js" src="js/libs/require/require.js"></script>
    <link rel="stylesheet" type="text/css" href="stylesheets/style.css">
</head>

<body>

<div id="container">

    <div id="menu"></div>

    <div id="content"></div>

</div>
</body>
</html>

main.js

require.config({
    paths:{
        jquery:'libs/jquery/jquery-min',
        underscore:'libs/underscore/underscore-min',
        backbone:'libs/backbone/backbone-min',
        templates:'../templates'
    }
});

require(['app'], function (App) {
    console.log('____main.js')
    App.initialize();
    console.log('main.js')
});

应用程序.js

define(['jquery', 'underscore', 'backbone', 'router'], function($, _, Backbone, Router) {
    var initialize = function(){
        Router.initialize();
        console.log('app.js');
    };

    return {
        initialize: initialize
    };
});

路由器.js

define([
    'jquery',
    'underscore',
    'backbone',
    'views/home/login'
    //'views/room',
//    'views/friend',
//    'views/play',
//    'views/money'
], function ($, _, Backbone, loginView) {
    console.log('router.js');
    var AppRouter = Backbone.Router.extend({
        routes:{
            '/home':'showHome',
            '*actions':'defaultAction'
        },
        showHome:function () {
            loginView.render();
        },
        defaultAction:function (actions) {
            console.log('No route:', actions);
        }
    });

    var initialize = function(){
        var app_router = new AppRouter;
        Backbone.history.start();
    };
    return {
        initialize: initialize
    };
});

login.js(主干视图)

define([
    'jquery',
    'underscore',
    'backbone',
    'collections/home/users',
    'text!templates/home/login.html'
], function($, _, Backbone, usersCollections, homeLoginTemplate){
    var loginView = Backbone.View.extend({
        el: $('container'),
        render: function(){
            var data = {};
            var compiledTemplate = _.template(homeLoginTemplate);
            this.el.append(compiledTemplate);
        }
    });
    console.log('login.js');
    return new loginView;
});

user.js(骨干模型)

define([
    'underscore',
    'backbone'
], function (_, Backbone) {
    console.log('user model');
    var userModel = Backbone.Model.extend({
        default:{
            username:"",
            password:""
        }
    });

    return userModel;
});

users.js(主干集合)

define(['underscore', 'backbone', 'models/user'], function (_, Backbone, userModel) {
    console.log('user collection');
    var usersCollection = Backbone.Collection.extend({
        model:userModel
    });
    return new usersCollection;
});

我不能发我的照片。但是您可以在此链接中看到http://imageshack.us/photo/my-images/809/devi.jpg/

4

2 回答 2

5

也许您的 main.js 文件中缺少一些 shim 配置,请在此处阅读:http ://requirejs.org/docs/api.html#config-shim

您可以尝试添加:

shim: {
    'backbone': {
        deps: ['jquery','underscore'],
        exports: 'Backbone'
    }
}
于 2013-01-31T08:32:54.903 回答
5

使用@Ingro 所说的,并在调用渲染模板时尝试使用默认的下划线标记。

例如:

main.js

require.config({
    paths:{
        jquery:'libs/jquery/jquery-min',
        underscore:'libs/underscore/underscore-min',
        backbone:'libs/backbone/backbone-min',
        templates:'../templates'
    },

    shim: {
        backbone: {
            deps: ['jquery','underscore'],
            exports: 'Backbone'
        }
    }
});
require(['app'], function (App) {
    console.log('____main.js')
    App.initialize();
    console.log('main.js')
});

并在您使用下划线模板的任何文件中,例如您的“login.js”使用此:

define([
    'backbone',
    'collections/home/users',
    'text!templates/home/login.html'
], function(Backbone, usersCollections, homeLoginTemplate){
    var loginView = Backbone.View.extend({
        el: $('container'),
        render: function(){
            var data = {};
            var compiledTemplate = _.template(homeLoginTemplate);
            this.el.append(compiledTemplate);
        }
    });
    console.log('login.js');
    return new loginView;
});

jquery 和 underscore 都默认在主干需要调用中使用。我希望这些对你有帮助。

于 2013-04-28T17:02:33.597 回答