Javascript 我需要重写backbone.sync以允许PUT方法
我需要覆盖Backbone.sync以允许放置问题是我不知道如何以及在哪里放置它 这是我的模型:Javascript 我需要重写backbone.sync以允许PUT方法,javascript,backbone.js,underscore.js,requirejs,Javascript,Backbone.js,Underscore.js,Requirejs,我需要覆盖Backbone.sync以允许放置问题是我不知道如何以及在哪里放置它 这是我的模型: define([ 'underscore', 'backbone' ], function(_, Backbone) { var Input = Backbone.Model.extend({ url: 'http://localhost/InterprisePOS/SOP/mobilecreateinvoice/', initialize: function(){ }, toJSON : fun
define([
'underscore',
'backbone'
], function(_, Backbone) {
var Input = Backbone.Model.extend({
url: 'http://localhost/InterprisePOS/SOP/mobilecreateinvoice/',
initialize: function(){
},
toJSON : function() {
return _.clone({ input:this.attributes });
},
});
return Input;
});
这是我视图中的“保存”功能:
save: function(){
invoice = new Invoice();
input = new Input();
invoice.set({POSWorkstationID: "POS7"});
invoice.set({POSClerkID: "admin"});
invoice.set({CustomerName: "Alice in Wonderland Tours"});
invoice.set({IsFreightOverwrite: true});
invoice.set({BillToCode: "CUST-000009"});
InvoiceCollection.add( invoice );
//var invoices = JSON.stringify({Invoices: InvoiceCollection.toJSON()});
_.each(this.collection.models, function(cart){
InvoiceDetailCollection.add( cart );
});
input.set({ "Invoices": InvoiceCollection.toJSON() });
input.set({ "InvoiceDetails": InvoiceDetailCollection});
alert( JSON.stringify(input.toJSON()) );
input.save();
}
默认主干同步处理程序将CRUD映射到REST,如下所示:
Backbone.sync = function(method, model, options, error) {
// Backwards compatibility with Backbone <= 0.3.3
if (typeof options == 'function') {
options = {
success: options,
error: error
};
}
var resp = function(resp) {
if (resp.status) {
options.success(method != 'read' ? model : resp.data);
}
else {
options.error('Record not found ' + resp.data);
}
};
var store = model.customStorage || model.collection.customStorage;
switch (method) {
case 'read': model.id ? store.read({id: model.id}, resp) : store.readAll(resp); break;
case 'create': store.create(model.attributes, resp); break;
case 'update': store.update(model.attributes, resp); break;
case 'delete': store.delete(model.id, resp); break;
}
};
- 创造→ 邮寄/收集
- 阅读→ 获取/收集[/id]
- 更新→ PUT/collection/id
- 删除→ 删除/收集/id
Backbone.emulateHTTP
设置为true
如果需要自定义映射,则需要覆盖Backbone.sync。覆盖的示例如下所示:
Backbone.sync = function(method, model, options, error) {
// Backwards compatibility with Backbone <= 0.3.3
if (typeof options == 'function') {
options = {
success: options,
error: error
};
}
var resp = function(resp) {
if (resp.status) {
options.success(method != 'read' ? model : resp.data);
}
else {
options.error('Record not found ' + resp.data);
}
};
var store = model.customStorage || model.collection.customStorage;
switch (method) {
case 'read': model.id ? store.read({id: model.id}, resp) : store.readAll(resp); break;
case 'create': store.create(model.attributes, resp); break;
case 'update': store.update(model.attributes, resp); break;
case 'delete': store.delete(model.id, resp); break;
}
};
Backbone.sync=函数(方法、模型、选项、错误){
//向后与主干网的兼容性等待。我应该在哪里编辑以覆盖Backbone.sync?我应该如何在视图中调用它?感谢您正在替换Backbone.sync
,请注意“Backbone.sync=”,所以您应该在包含主干库之后放置它。抱歉,但我只想澄清一下。因为put已经启用。我是否需要覆盖Backbone.sync?如果我想使用put,我可以使用类似的东西吗?我将如何称呼它?如果您只需要put,主干已经提供了put,因为默认的同步处理程序是maps CRUD到REST。所以您所做的就是更新模型。所以只需调用myModel.update,默认情况下它将在您的服务器上执行PUT。