【问题标题】:Making Backbone.js modular with Require.js使用 Require.js 使 Backbone.js 模块化
【发布时间】:2013-04-12 13:09:55
【问题描述】:

我正在尝试通过 Backbone.js 使我的 Backbone.js 组件、模型和视图模块化。但是,每当我尝试require() 时,它都会返回:

function (){return i.apply(this,arguments)} 

而不是我需要的 Backbone 魔法。

我已经像这样设置了我的 Require.js:

// Configure require.js
require.config(
    {
        paths: {
            "data": "config"
        ,   "jquery": "libs/jquery.min"
        ,   "underscore": "libs/underscore-min"
        ,   "backbone": "libs/backbone-min"
        ,   "events": "collections/events"
        ,   "eventModel": "models/eventModel"
        ,   "eventView": "views/eventView"
        }
    ,   shim: {
            underscore: {
                exports: '_'
            }
        ,   jquery: {
                exports: '$'
            }
        ,   backbone: {
                deps: ["underscore"]
            ,   exports: "Backbone"
            }
        }
    }
);

// Initiate app
require(
    [
        "data"
    ,   "jquery"
    ,   "underscore"
    ,   "backbone"
    ,   "events"
    ,   "eventModel"
    ,   "eventView"
    ], function(data, $, _, Backbone, Events, EventModel, EventView) {
        console.log(Events);
        _.each(["data/general.xml"], function(URI) {
            var general = new Events({url: URI});
        });
    }
);

这是我的 collections/events.js:

define(
    [
        "backbone"
    ,   "eventModel"
    ]
,   function(Backbone, EventModel) {
        "use strict";
        var Events = Backbone.Collection.extend({
            model: EventModel

        ,   parse: function(data) {
                var parsed = [];
                $(data).find('Event').each(function(index) {
                    parsed.push({
                        title: $(this).find('title').text(),
                        date: $(this).find('date').text(),
                        content: $(this).find('content').text()
                    });
                });
                return parsed;
            }

        ,   fetch: function(options) {
                options = options || {};
                options.dataType = "xml";
                Backbone.Collection.prototype.fetch.call(this, options);
            }
        });

        return Events;
    }
);

我希望返回 Events 集合,但显然不是。谁能看到我做错了什么?

【问题讨论】:

    标签: javascript backbone.js requirejs


    【解决方案1】:

    一切似乎都很好。你所看到的:

    function (){return i.apply(this,arguments)} 
    

    是你的类的构造函数。不要理会它。相反,请尝试登录 new myClass

    编辑:
    您看不到任何方法,因为它们存储在您的类的 prototype 中。被调用的 i 函数是“真正的”构造函数(命名为 i 因为它已被缩小)。

    【讨论】:

    • 你是对的,我试图在后面错误地使用它,并认为这是问题所在。刚刚注意到我的错误。傻我。
    猜你喜欢
    • 1970-01-01
    • 2019-01-20
    • 2014-05-28
    • 2013-01-21
    • 2012-06-11
    • 1970-01-01
    • 1970-01-01
    • 2012-06-30
    • 1970-01-01
    相关资源
    最近更新 更多