1

我正在使用 RequireJS 来处理我的所有依赖项。

所以这是我的观点:

define([
    'jquery',
    'underscore',
    'backbone',
    'bootstrap',
    'collections/InstitutionsCollection'
], function($, _, Backbone, InstitutionsCollection){

    var InstitutionsView = Backbone.View.extend({
        render: function() {
            var institutions = new InstitutionsCollection();

            institutions.fetch({
                success: function() {
                    console.log("success!");
                }
            });
        }
    });

    return InstitutionsView;
});

此行引发错误: var institutions = new InstitutionsCollection();

这是我的收藏:

define([
    'jquery',
    'underscore',
    'backbone',
    'models/InstitutionModel'
], function($, _, Backbone, InstitutionModel){

    var InstitutionsCollection = Backbone.Collection.extend({
        model: InstitutionModel,
        url: '/institutions/'
    });

    return InstitutionsCollection;
});

如果你需要它,这是我的模型:

define([
    'jquery',
    'underscore',
    'backbone'
], function($, _, Backbone){

    var InstitutionModel = Backbone.Model.extend({

    });

    return InstitutionModel;
});

我已经盯着它看了一会儿,我只是想不通它为什么会抛出那个错误。任何帮助是极大的赞赏。

4

3 回答 3

4

正如其他人所建议的那样,您的define回调参数顺序不正确匹配。我想指出有助于缓解这种常见错误的糖语法:

define(function(require) {
    var $ = require('jquery');
    var _ = require('underscore');
    var Backbone = require('backbone');
    var InstitutionsCollection = require('collections/InstitutionsCollection');
    require('bootstrap');

    var InstitutionsView = Backbone.View.extend({
        // Code
    });

    return InstitutionsView
});

在文档中阅读有关它的更多信息。

于 2013-04-12T21:58:42.767 回答
3

当您定义您的时,view您正在导入引导程序并将其映射到InstitutionsCollectionrequire 回调中的参数。尝试按如下方式更新您的代码:

define([
    'jquery',
    'underscore',
    'backbone',
    'collections/InstitutionsCollection',
    'bootstrap'
], function($, _, Backbone, InstitutionsCollection){

    var InstitutionsView = Backbone.View.extend({
        render: function() {
            var institutions = new InstitutionsCollection();

            institutions.fetch({
                success: function() {
                    console.log("success!");
                }
            });
        }
    });

    return InstitutionsView;
});

保持映射同步真的很痛苦,而且在扫描代码时很容易被忽略!

于 2013-04-12T21:37:02.117 回答
2

您正在映射bootstrapInstitutionsCollection. 将其更改为:

define([
  'jquery', 'underscore', 'backbone', 'collections/InstitutionsCollection',           
  'bootstrap'
], function($, _, Backbone, InstitutionsCollection){});
于 2013-04-12T21:36:22.223 回答