2013-07-11 80 views
2

我正在使用backbone.js以及jquery和underscore.js。
錯誤說:
類型錯誤:CollectorCollection是不是構造
變種收藏家=新CollectorCollection();
RequireJs骨幹錯誤集合在創建新集合時不是構造函數

這是我的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
  • 在需要時(如果你做正確的事情,不要在全局命名空間編寫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; 
}); 
+0

謝謝,我已經糾正的大寫的失敗,而不是原因。 – mstafkmx

+0

我只注意到你爲你的模塊定義了名字(你的第一個參數),這可能也是問題的一部分。我修改了集合的代碼作爲示例 – nikoshr

+0

@MustaphaKoumach在模塊定義中刪除名稱有幫助嗎? – nikoshr