2014-10-29 2 views
1

서버로 아약스 요청을하려고합니다. 나는 컬렉션에 fetch() 메서드를 사용하고 있습니다. 그것은 성공적으로 Google 크롬 네트워크 탭을 모니터링 후 서버에서 데이터를 검색합니다.each() 함수가 this.collection에 정의되어 있지 않습니다.

여기에 시나리오가 나와 있습니다.

나는 Model

var Model = Backbone.Model.extend({ 
    urlRoot: '/contacts', 
}); 

collection 아래 ViewTwo

입니다

var Collection = Backbone.Collection.extend({ 
    model: Model, 
    url: '/contacts' 
}); 

ViewOne

var ViewOne = Backbone.View.extend({ 
    initialize: function() { 
     var collection = new Collection().fetch(); // also tried with these options { wait:true, reset: true, async: false } 
     new ViewTwo({ collection: collection }); 
    } 
}); 

아래 아래

아래에 있습니다
var ViewTwo = Backbone.View.extend({ 
    initialize: function() { 
     this.collection.each(this.render, this); // Uncaught TypeError: undefined is not a function 
    }, 

    render: function() { 
     console.log(this.model); 
    } 
}); 

알다시피 나는 Uncaught TypeError: undefined is not a function 오류가 발생합니다.

this.render이 있으므로 문제가되지 않습니다. 문제는 각각 this.collection의 기능입니다. 어떻게이 문제를 해결할 수 있습니까?

답변

1

백본 API를 가져 오기의 성공 콜백에서 새보기를 만들 수 있습니다.

var ViewOne = Backbone.View.extend({ 
    initialize: function() { 
     var collection = new Collection(); 
     collection.fetch(); 

     new ViewTwo({ collection: collection }); 
    } 
}); 

을 시도하고 collection.fetch가 비동기주의, this.collectionViewTwo.initialize에서 아마 비어 있습니다. 예를 들어 Backbone.js - data not being populated in collection even though fetch is successful을 참조하십시오.

2

단순히는 fetch에 사용되는 아약스의 객체를 반환, Collection.fetch 자체를 반환하지 않는 것을 의미한다 유창하지

var ViewOne = Backbone.View.extend({ 
    initialize: function() { 
     var collection = new Collection(); 
     collection.fetch({ 
     success:function(){ 
      new ViewTwo({ collection: collection }); 
      } 
      }); 
    } 
}); 
+0

그래도 문제가 해결되지 않습니다. 여전히 같은 오류. –

+0

@undefinedcorvus 오류를 복제하기위한 피들 생성 – StateLess

관련 문제