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