2014-01-20 3 views
0

백본을 배우고 있는데 누군가가이 문제를 해결할 수 있다면 도움이 될 것입니다. 내 콜렉션에 대한 반입을 수행 한 후에 성공 콜백에서 collection.toJSON()을 사용하여 구문 분석 된 데이터를 얻습니다. 실제로이 오브젝트가 반환되며이 오브젝트에서 아무 것도 얻을 수 없습니다. 이 객체에는 실제로 필요한 데이터가 있습니다.Backbone.js collectio.toJSON()의 응답을 구문 분석 할 수 없습니다.

Please see the object contents in this screenshot

내 질문은 내 객체의 행 속성에 액세스 할 방법이다. 여기 toJSON가 당신에게 각 개체가 Model's JSON 객체 인 JSON 개체의 배열을 반환, 제안 이름으로 참조

var testCollection = Backbone.Collection.extend({ 
    model:myModel, 
    url: '/myApiEndPoint', 
    data: '', 
    initialize: function(models, options) { 
     this.data = models.data; 
    }, 
    fetch: function(options) { 
     var ajaxConfig = { 
      url: this.url, 
      data: this.data, 
      type: 'POST', 
      dataType: 'text', 
      contentType: 'text/xml', 
      parse: true 
     }; 
     options = _.extend({}, ajaxConfig, options); 

     return Backbone.Collection.prototype.fetch.call(this, options); 
    }, 
    parse: function(xmlResponse) { 
      // I have some parsing logic to extract uid and rows from my xmlResponse 
      return { 
       uid: uid, 
       rows: rows 
      }; 
     }, 
    }); 

var collObj = new testCollection({data: xmlQuery1}); 
collObj.fetch({ 
    success: function(collection){ 
    // This code block will be triggered only after receiving the data. 
    console.log(collection.toJSON()); 
    } 
}); 

답변

1

내 코드입니다. 다음과 같이 필요한 속성을 가져올 수 있습니다.

collObj.fetch({ 
    success: function(collection){ 
    // This code block will be triggered only after receiving the data. 
    console.log(collection.toJSON()); 
    var uid = 'uid-of-an-object-to-access'; 
    var rows = collection.get(uid).get('rows'); 
    console.log(rows); 
    } 
}); 
관련 문제