2017-05-07 4 views
-1

午後すべて私はバックボーンには比較的新しいので、これまで見たことがないこのエラーで3日間困惑しました。バックボーンエラー:モデルはコンストラクタではありません

私は関数としてモデルを定義するコレクション 'TestCollection'を持っています。コレクションがロードされると、クラス 'TestModel'でモデルを作成しようとするとエラーが発生します。

私が手にエラーがある:

Uncaught TypeError: TestModel is not a constructor 
at new model (testCollection.js:14) 
at child._prepareModel (backbone.js:913) 
at child.set (backbone.js:700) 
at child.add (backbone.js:632) 
at child.reset (backbone.js:764) 
at Object.options.success (backbone.js:860) 
at fire (jquery.js:3143) 
at Object.fireWith [as resolveWith] (jquery.js:3255) 
at done (jquery.js:9309) 
at XMLHttpRequest.callback (jquery.js:9713) 

私は、私が収集し、モデル、彼らが作業する必要があるのコードのすべての両方を与えていると考えています。それは何かがロードに間違っているように感じますが、モデルファイルの先頭にconsole.logを置くと、コレクションを使用しようとする前に確実にロードされていることがわかりました。

大変お手伝いをいたします。

TestCollection:

define([ 
    'backbone', 
    'models/testModel' 
], function(Backbone, TestModel) { 

    var TestCollection = Backbone.Collection.extend({ 

    model: function(attrs) { 
     switch (attrs._type) { 
     case 'test': 
      console.log('making a test model') 
      return new TestModel(); 
     } 
    }, 

    initialize : function(models, options){ 
     this.url = options.url; 
     this._type = options._type; 
     this.fetch({reset:true}); 
    } 

    }); 

    return TestCollection; 

}); 

TestModel:TestCollectionが行われ

require([ 
    './testParentModel' 
], function(TestParentModel) { 

    var TestModel = TestParentModel.extend({ 

    urlRoot: 'root/url', 

    initialize: function() { 
     console.log('making test model') 
    } 
    }); 

    return TestModel; 
}); 

ファイル:私は、スタックオーバーフロー周りを見て持っていた

define(function(require) { 

    var MyProjectCollection = require('collections/myProjectCollection'); 
    var TestCollection = require('collections/testCollection'); 

    Origin.on('router:dashboard', function(location, subLocation, action) { 



    Origin.on('dashboard:loaded', function (options) { 
    switch (options.type) { 
     case 'all': 
     var myProjectCollection = new MyProjectCollection; 

     myProjectCollection.fetch({ 
      success: function() { 
      myProjectCollection.each(function(project) { 

       this.project[project.id] = {}; 

       this.project[project.id].testObjects = new TestCollection([], { 
       url: 'url/' + project.id, 
       _type: 'test' 
       }); 
      }); 
      } 
     }); 
    } 
    }); 

}); 

、それは表示されません。以下の問題になります(これは最も一般的な問題です)。 Model is not a constructor-Backbone

また、私は循環依存性がないとも思います。

私は完全に困惑しているので、どんな助けも大いにありがとうございます。関連するコードのみを含めるようにしましたが、追加のコードが役立つかどうかお知らせください。

おかげ

+0

私がconsole.log(TestModel)にしようとすると、上記の行がコレクション内の新しいTestModel()を返し、 'undefined'が返されます。しかし、ファイルがブラウザのネットワークタブにロードされていることがわかります。これをデバッグする最善の方法は何ですか? – user3707803

+0

[同じコレクションに異なるモデルタイプを保存する方法](http://stackoverflow.com/a/40638871/1218980)。 –

答えて

1

私は、コードの他の部分のために言うことはできませんが、あなたは持っている明らかな問題は、モデル作成者の関数に渡されるどのようなデータ誤解されます。

var TestCollection = Backbone.Collection.extend({ 
    model: function(attrs) { 
     switch (attrs._type) { // attrs._type does exist here 
      console.log(attrs); // Will print { foo: 'bar' } 

      case 'test': // This will always be false since attrs._type does not exist 
       console.log('making a test model') 
       return new TestModel(); 

      default: 
       return new Backbone.Model(); // Or return some other model instance, 
              // you MUST have this function return 
              // some kind of a Backbone.Model 
     } 
    }, 

    initialize : function(models, options){ 
     this.url = options.url; 
     this._type = options._type; 
     this.fetch({reset:true}); 
    } 
}); 

new TestCollection([ { foo: 'bar' }], { 
    url: 'url/' + project.id, 
    _type: 'test' // This will NOT be passed to the model attrs, these are 
        // options used for creating the Collection instance. 
}) 

再実行するにはCollectionをインスタンス化するときは、普通のオブジェクト配列[{ foo: 'bar'}, { foo: 'baz'}]を渡します(または、あなたが行っているようにfetch経由で取得します)。そのオブジェクトはmodel関数のattrsパラメータとして渡され、model関数は少なくともステートメントのフォールバックが必要なBackbone.Modelインスタンスを返さなければなりません。

関連する問題