我如何防止extjs模型/代理在创建时保存空的主ID [英] how do i prevent an extjs model/proxy from saving the empty primary id on create

查看:131
本文介绍了我如何防止extjs模型/代理在创建时保存空的主ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用REST代理定义了我的模型。它对于读取(GET)和更新(PUT)可以正常工作,因为这些操作需要一个主ID。当我执行创建操作(POST)时,代理将所有字段(包括空的主ID)发送到服务器,这导致服务器和错误。服务器期望为创建操作不包括主要的id。我如何指示extjs不发送空的主id值?即。 {'model_id':'',...}?

  Ext.define('model',{
extends:'Ext.data.Model',
idProperty:'model_id',
fields:['model_id','first','last'],
proxy:{
类型:'rest'
}
});

var mymodel = Ext.create('model',{last:'digler'});
mymodel.save()// posts{'model_id':'','last':'digler'}?

我希望它不会在创建时包含主id字段。

解决方案

我认为这是更改请求结构的错误方式。在使用REST的情况下,操作创建,更新请求的责任完全在服务器端。



如果您仍然想在创建请求启动时更改参数你可以通过beforerequest事件来执行:

  Ext.Ajax.on(beforerequest,function(conn,options ,eOpts){
if(options.action =='create')
{
var newData =
{'name':options.jsonData.name,'e​​mail': options.jsonData.email};
options.jsonData = newData;
}
});

只是例如我重新定义硬编码的数据字段,但当然可以运行通过循环中的所有字段,而不在代码中写入其名称。


i have defined my model with a REST proxy. it works fine for read (GET) and update (PUT) because those operations require a primary id. when i perform a create operation (POST) the proxy sends all the fields, including the empty primary id, to the server, which causes and error on the server. the server expects that no primary id is to be included for a create operation. how do i instruct extjs to not send the empty primary id value? ie. "{ 'model_id':'',...}"?

Ext.define('model', {
    extend : 'Ext.data.Model',
    idProperty : 'model_id',
    fields : ['model_id', 'first', 'last'],
    proxy : {
         type : 'rest'
    }
});

var mymodel = Ext.create('model',{last:'digler'});
mymodel.save() //posts "{ 'model_id':'', 'last':'digler'}"?

i want it to not include the primary id field at all on a create.

解决方案

I think that this is a wrong way to change the structure of request. In the case of usage REST, the responsibility to operate create, update requests are fully on server side.

In the case you still want to change parameters when create request is started, you can do it by "beforerequest" event:

Ext.Ajax.on("beforerequest", function( conn, options, eOpts){
  if (options.action=='create')
  {
    var newData = 
           {'name': options.jsonData.name, 'email': options.jsonData.email }; 
    options.jsonData = newData;
  }
});

Just for example I re-defines data fields hard coded, but, of course, it's possible to run through all fields in loop without writting its names in code.

这篇关于我如何防止extjs模型/代理在创建时保存空的主ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆