在sencha touch 2中,对现有记录的model.save()调用正在按预期触发POST而不是PUT。
我有以下型号:
Ext.define('HomeInventory.model.Product', {
extend: 'Ext.data.Model',
config: {
idProperty: '_id',
fields: [
{ name: '_id', type: 'auto' },
{ name: 'name', type: 'string' },
{ name: 'barcode', type: 'string' },
{ name: 'creationDate', type: 'date' },
{ name: 'currentAmount', type: 'number' },
{ name: 'isActive', type: 'boolean'}
],
validations: [
{type: 'presence', field: 'barcode', message: 'Barcode is required'},
{type: 'presence', field: 'name', message: 'Name is required'}
],
proxy :{
type: 'rest',
url: 'http://localhost:3000/products',
actionMethods: {
create: 'POST',
read: 'GET',
update: 'PUT',
destroy: 'DELETE'
},
}
}
});
json有效负载包含现有记录所需的_id字段,但是使用HTTP POST而不是PUT发送到服务器:
{_id: "575bcd86c0eb22880c7e421e", name: "test product1", barcode: "1234", creationDate: null,…}
在控制器内保存函数调用:
submitProduct: function(){
Ext.Viewport.setMasked({
xtype: 'loadmask',
indicator: true,
message: 'Saving product...'
});
debugger;
var product = Ext.create('HomeInventory.model.Product');
this.getProductView().updateRecord(product);
var validation = product.validate();
if(validation.isValid){
var me = this;
product.save({
success: function(){
Ext.Viewport.unmask();
me.returnToMain();
},
failure: function(){
Ext.Viewport.unmask();
Ext.Msg.alert('There was an error updating the product');
me.returnToMain();
}
});
}else{
//Show validation error
}
}
这里有什么问题?
答案 0 :(得分:2)
好吧,我找到了解决方案。在调用save方法之前,有必要在产品上设置phantom = false
。