RequireJs Backbone error在创建新Collection时,Collection不是构造函数

时间:2013-07-11 14:02:57

标签: backbone.js requirejs

我正在使用backbone.js以及jquery和underscore.js 错误说:
TypeError:CollectorCollection不是构造函数
var collectors = new CollectorCollection();

这是我的index.html

<html><heade></head><body><script data-main="js/mainCollector" src="js/libs/require.js"></script></body>

mainCollector.js

    require.config({
    paths: {
        html5shiv: "libs/html5shiv",
        jquery: "http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min",
        jqueryui: "http://code.jquery.com/ui/1.10.3/jquery-ui",
        tablesorter: "libs/jquery.tablesorter.min",
        script: "script",
        underscore: "libs/underscore.min", /*"http://underscorejs.org/underscore",*/
        backbone: "libs/backbone.min", /*"http://backbonejs.org/backbone-min",*/
        utils: "utils",
        collectorModel: "models/collectorModel",
        collectorCollection: "collectorCollection",
        collectorRouter: "collectorRouter",
        edit: "views/Collector/collector_edit",
        index: "views/Collector/collector_index",
        neww: "views/Collector/collector_new",
        row: "views/Collector/collector_row",
        show: "views/Collector/collector_show"
    },
    shim: {
        jqueryui: {
            deps: ["jquery"],
            exports: "Jqueryui"
        },
        tablesorter: {
            deps: ["jquery"],
            exports: "TableSorter"
        },
        script: {
            deps: ["jquery"],
            exports: "Script"
        },
        underscore: {
            exports: "_"
        },
        backbone: {
            deps: ["underscore", "jquery"],
            exports: "Backbone"
        }
    }    
});    
require(["backbone", "underscore", "collectorCollection", "collectorRouter"],
    function (Backbone, _, CollectorCollection, CollectorRouter) {
        var Collectors = new CollectorCollection();
        var router = new CollectorRouter({collectors: collectors});
        console.log("Start App");
        Backbone.history.start();
    });

colectorCollection.js

 define("collection", [
        "underscore",
        "backbone",
        "collectorModel"
    ],  function(_, Backbone, CollectorModel) {
        console.log("Collection loaded");
        var CollectorCollection = Backbone.Collection.extend({
            // Reference to this collection's model.
            url: "api/index.php/Collectors",
            model: CollectorModel
        });
        return CollectorCollection;
    });

collectorModel.js

define("model", ["underscore", "backbone"],  function(_, Backbone){
    console.log("model loaded");

    var CollectorModel = Backbone.Model.extend({
        urlRoot: 'api/index.php/collectors',
        // the root
        paramRoot: "collector",

        // the default fields
        defaults: {
            id: null,
            name: ""
        }
    });
    return CollectorModel;
});

感谢。

1 个答案:

答案 0 :(得分:1)

  • Javascript区分大小写
  • 定义文件中使用的变量名称是定义的本地变量,在需要时它们将无法使用(如果您正确地执行操作并且不在全局命名空间上写入),
  • define names为你的模块,这可能会导致问题

这意味着CollectorCollection在全球范围内无法使用,而在

中可用
require(["collectorCollection"], function (collectorCollection) {
}

您的收藏实际上是collectorCollection:请注意小写c。

因此,您的需求电话可以写成

require(["backbone", "underscore", "collectorCollection", "collectorRouter"],
    function (Backbone, _, CollectorCollection, CollectorRouter) {
        var collectors = new CollectorCollection();
        var router = new CollectorRouter({collectors: collectors});
        console.log("Start App");
        Backbone.history.start();
    });

您的收藏定义中存在类似的问题:

define([
        "underscore",
        "backbone",
        "collectorModel"
    ],  function(_, Backbone, CollectorModel) {
        console.log("Collection loaded");
        var CollectorCollection = Backbone.Collection.extend({
            // Reference to this collection's model.
            url: "api/index.php/Collectors",
            model: CollectorModel
        });
        return CollectorCollection;
});