2017-05-07 3 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 인스턴스를 반환해야합니다. switch 문에 대한 대체가 필요합니다.

관련 문제