저는 Backbone을 배우고 있는데 누군가가이 문제를 도와 줄 수 있다면 좋을 것입니다. 내 컬렉션에 대한 가져 오기를 수행 한 후 성공 콜백에서를 사용하여 구문 분석 된 데이터를 얻습니다 collection.toJSON()
. 이것은 실제로 객체를 반환하며이 객체에서 아무것도 가져올 수 없습니다. 이 개체에는 실제로 필요한 데이터가 있습니다.
내 질문은 내 개체의 행 속성에 액세스하는 방법입니다. 다음은 참조 용 코드입니다.
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());
}
});
이름에서 toJSON
알 수 있듯이 각 개체가 JSON 개체 인 JSON 개체 배열을 반환 Model's
합니다. 다음과 같은 방법으로 필수 속성을 가져올 수 있습니다.
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);
}
});
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다