ext direct spring Store Modify Method

来源:互联网 发布:肉毒素 腋臭 知乎 编辑:程序博客网 时间:2024/06/05 06:37


Server

Create/Update

负责处理从DirectStore发出的创建,更新和销毁请求的方法添加注解@ExtDirectMethod(value = ExtDirectMethodType.STORE_MODIFY)。此类方法可以有一个集合类型参数或单个

对象。不能两个名称一样的方法都用@ExtDirectMethod注解。

方法的返回类型跟参数类型相同或是ExtDirectStoreResult实例。当reader配置了root属性时方法返回一个对象实例。

集合参数例子:

 @ExtDirectMethod(value = ExtDirectMethodType.STORE_MODIFY)  public List<Person> createOrUpdate(List<Person> newOrUpdatedPersons) {    List<Person> result = Lists.newArrayList();    for (Person person : newOrUpdatedPersons) {      //for example insert/update person into database      //set the id attribute if it's an insert and add it to the result collection      result.add(person);    }    return result;  }


对象作为参数例子:

 @ExtDirectMethod(value = ExtDirectMethodType.STORE_MODIFY)  public Person createOrUpdate(Person person) {    //insert or update person    //set id if it's an insert    return person;  }


在DirectStore中的数据在服务器更新后返回。因此,服务器方法有机会通过简单的返回更改数据或什么都不返回。一个常见的用例是数据库插入后返回主键对象。

返回一个集合或是一个对象是有客户端决定的。例如如果store的autoSync设置为disabled,那么用户可以修改多条记录而不像服务器发送任何数据。当手动同步store是发送多条记

录到服务器。在这中情况服务器方法需要一个集合参数接收所有修改或插入的多行记录。在另外一中情况,如果autoSync设置的值true store 会发送修改或插入的数据到服务器端

每次用户修改或插入数据的时候,在这中情况服务器端每次只处理一个对象。一个对象方法也适用于没有store,而从Model类调用save()或destroy()的情形。

如果客户端设置了reader的root属性,服务器方法返回一个对象实例,对象实例包装为ExtDirectStoreResult。

Ext.define('Person', {  extend : 'Ext.data.Model',  fields : ...,  proxy : {    type: 'direct',    api: {      ...    },    reader : {      root : 'records'    }  }});@ExtDirectMethod(STORE_MODIFY)public ExtDirectStoreResult<User> create(User newUser) {    //do something with newUser    return new ExtDirectStoreResult<>(newUser);}

包装不是必须的,当方法返回一个集合时,Extj都能正确处理,无论reader带或不带root属性。

Destroy Ext JS 3.x

跟create和update方法相比,destroy方法只接收一个id的集合,并返回一个id的集合。

@ExtDirectMethod(value = ExtDirectMethodType.STORE_MODIFY, entryClass = Integer.class)  public List<Integer> destroy(List<Integer> destroyIds) {    List<Integer> deletedPersonsId = Lists.newArrayList();    for (Integer id : destroyIds) {      ...      deletedPersonsId.add(id);    }    return deletedPersonsId;  }

该方法可以返回一个空的集合否决destroy请求。


Destroy Ext JS 4.x and Sencha Touch 2

在Ext JS的4.x的的destory方法运行方式发生改变,客户端发送整个对象到服务器端就像create和update方法。好像不能否决destory方法了在4.x版本中。虽然服务器端可以忽略掉

deleted请求,但如果他没重新加载数据,用户不能够在看见删除的记录了。

@ExtDirectMethod(STORE_MODIFY)  public void destroy(List<User> destroyUsers) {  for (User user : destroyUsers) {    //delete the user from the persistent storage     //or ignore the request  }}

Client Ext JS 3.x

在客户端需要一个JsonWriter的实例。JsonWriter的listful的属性必须设置为true。这种方式writer会一直发送一个list到服务器,甚至虽然只有一条记录插入,更新或销毁。

encode属性必须设置为false。如果writeAllFields设置为true,记录的所有字段都会发送到服务器,如果设置为false,只有改变的字段发送到服务器。

 var writer = new Ext.data.JsonWriter( {    writeAllFields: true,    listful: true,    encode: false  });

在API中配置create/read/update/delete方法。

var directStore = new Ext.data.DirectStore( {    id: 'directStore',    paramsAsHash: true,    root: 'records',    totalProperty: 'total',    autoLoad: false,    autoSave: true,    successProperty: 'success',    fields: ['id', 'firstName', 'lastName'],    remoteSort: true,    idProperty: 'id',    writer: writer,    api: {      read: personAction.load,      create: personAction.createOrUpdate,      update: personAction.createOrUpdate,      destroy: personAction.destroy    }  });

Client Ext JS 4.x and Sencha Touch 2

在Extjs 4.x中配置DirectStore发生一些变化,虽然仍然可以在DirectStore配置一切属性,但首选的方式是在模型类中配置代理对象。

Ext.define('Person', {  extend : 'Ext.data.Model',  fields : [ 'id', 'firstName', 'lastName' ],  proxy : {   type: 'direct',    api: {      read: personAction.load,      create: personAction.createOrUpdate,      update: personAction.createOrUpdate,      destroy: personAction.destroy    },    reader : {     root : 'records'    }  }});var store = Ext.create('Ext.data.Store', {  autoLoad: false,  autoSave: true,  model : 'Person'});

Examples

http://demo.rasc.ch/eds/extjs3/grid.html
http://demo.rasc.ch/eds/extjs42/rowedit.html
http://demo.rasc.ch/eds/extjs42/roweditpaging.html
http://demo.rasc.ch/eds/extjs41/rowedit.html
http://demo.rasc.ch/eds/extjs41/roweditpaging.html



























0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 老人吃饭噎着了怎么办 胃胀气嗝不出来怎么办 嗓子咽口水都疼怎么办 产后盆底肌肉松弛怎么办 2个月宝宝鼻塞怎么办 人淹死捞不上来怎么办 胶囊卡在胃里怎么办 药卡在气管里怎么办 胶囊药卡在气管怎么办 被胶囊卡在喉咙怎么办 药卡在食道里怎么办 胶囊黏在喉咙里怎么办 要一直卡在喉咙怎么办 胃老是往上反气怎么办 有口气憋在喉咙怎么办 肛裂伤口不愈合怎么办 肛裂口子不愈合怎么办 宝宝胃食道反流怎么办 去角质后脸发红怎么办 红烧肉做的太甜怎么办 红烧排骨太甜了怎么办 唱歌时嗓子有痰怎么办 一唱歌喉咙有痰怎么办 鼻子老是打喷嚏还流鼻涕怎么办 鼻涕流到喉咙里怎么办 鼻塞怎么办怎样让鼻通气 流清鼻涕嗓子疼怎么办 喉咙疼咳嗽有痰怎么办 扁桃体发炎痛得厉害怎么办 腭垂掉下来了怎么办 喉咙干有异物感怎么办 嗓子干有异物感怎么办 输液的时候手疼怎么办 一感冒就嗓子哑怎么办 4岁儿童喉咙沙哑怎么办 嗓子老有异物感怎么办 喉咙咽口水都疼怎么办? 舌头上长了溃疡怎么办 包包的拉链坏了怎么办 做试管取精困难怎么办 sw过膝靴往下掉怎么办