2

我正在使用backbone.js 以及jquery 和underscore.js。
错误说:
TypeError: CollectorCollection is not a constructor
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();
    });

收集器集合.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;
    });

收集器模型.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;
});

谢谢。

4

1 回答 1

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

这意味着它CollectorCollection不会在全球范围内可用,并且在

require(["collectorCollection"], function (collectorCollection) {
}

您的收藏实际上可以作为collectorCollection:注意小写 c。

因此,您的 require 调用可以写为

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;
});
于 2013-07-11T14:22:01.987 回答