我目前正在使用的其余界面有这样的内容,
返回一个集合
POST /base/Holiday/getHolidaySetup/
FORM_DATA: client_id:1
更糟糕的是..
POST /base/Holiday/setAddHoliday/
FORM_DATA: client_id:1
... other form data..
显然,在较新的计划中,它看起来像
/base/Holiday/client/
POST to create a new holiday.
/base/Holiday/client/
GET To retrieve holidays
但是,这不是我们正在使用的......
那么如何为这样的方案设置Backbone。
答案 0 :(得分:0)
您可以通过覆盖各自的sync
方法来完全更改模型和集合处理其持久性的方式。
例如,要获取集合
var M = Backbone.Model.extend({
idAttribute: "client_id"
});
var C = Backbone.Collection.extend({
model: M,
url: "/base/Holiday/getHolidaySetup/",
sync: function(method, model, options) {
var params = {
type: 'POST',
dataType: 'json',
data: JSON.stringify({client_id: 1}),
url: _.result(this, 'url'),
processData: false
};
var xhr = Backbone.ajax(_.extend(params, options));
model.trigger('request', model, xhr, options);
return xhr;
}
});
http://jsfiddle.net/7L9v5rkz/5/
或保存模型
var M = Backbone.Model.extend({
idAttribute: "client_id",
url: '/base/Holiday/setAddHoliday/',
sync: function(method, model, options) {
options = options || {};
var params = {
type: 'POST',
dataType: 'json',
processData: false
};
if ((method === 'create') || (method === 'update')) {
params.url = _.result(this, 'url');
params.data = JSON.stringify(this.toJSON(options));
} else {
// handle read and delete operations
}
var xhr = Backbone.ajax(_.extend(params, options));
model.trigger('request', model, xhr, options);
return xhr;
}
});