Backbone.Collection에서 '가져 오기'를 호출하면 차례대로 Backbone.sync가 호출됩니다. 기본적으로 Backbone.sync는 컬렉션에 사용하려는 URL을 묻습니다.
/models/batch/?ids=1,2,3,4
당신이 할 수있는 무엇인가 :
var MyCollection = Backbone.Collection.extend({
model: Model,
url: '/models',
initialize: function(models, options) {
ids = options.ids || [];
if (ids.length > 0) {
this.fetchByIds(ids);
}
},
fetchByIds: function(ids) {
// Save a reference to the existing url
var baseUrl = this.url;
// Assign our batch url to the 'url' property and call the server
this.url += '/?ids=' + ids.join(',');
this.fetch();
// Restore the 'url' property
this.url = baseUrl;
}
});
그래서처럼 사용
이
var coll = new MyCollection({}, {ids: [1, 2, 3, 4]});
당신은 통과해야 할 것
그래서 서버가 응답하면 Backbone.Collection 생성자 함수는 이전에 첫 번째 매개 변수로 전달 된 모델을 설정하기 때문에 options 매개 변수의 ID 그것은 '초기화'기능을 호출합니다.
이론적으로는 정상적으로 작동해야합니다 (읽지 않음 : 완전히 시도하지 않음).
대단히 감사합니다. – Joe