您的位置:首页 > Web前端 > JavaScript

ExtJS 4 DirectStore post参数的变化以及应对方法

2011-05-28 21:51 597 查看
还是Ext4,还是direct,这次是发送到后台参数的变化问题。

在Ext3中,继承Ext.data.DirectStore需要通过sortInfo属性指定排序字段和排序方法,通过paramOrder指定传入后台参数的顺序,例如:

var store = new Ext.data.DirectStore({
remoteSort: true,
directFn: Sample.Grid.PagingLoad,
paramOrder: ['sort', 'dir', 'start', 'limit'],
idProperty: 'id',
totalProperty: 'total',
root: 'data',
sortInfo: {
field: 'name',
direction: 'ASC'
},
fields: [{
type: 'int',
name: 'id'
}, 'name', {
type: 'int',
name: 'employees'
},{
type: 'float',
name: 'turnover'
},{
type: 'date',
name: 'started',
dateFormat: 'c'
}]
});


参数发送的方式是分别使用字符串post到后台,这是firebug相应信息:



后台通过设定好的参数顺序接受对应参数(均为string类型):

[DirectMethod]
public CompanySerializer PagingLoad(string order, string direction, long start, long limit)
{
CompanyCollection coll = new CompanyCollection();
coll.OrderField = order;
coll.OrderDirection = direction.ToLower() == "desc" ? CompanyCollection.Direction.DESC : CompanyCollection.Direction.ASC;

return new CompanySerializer(coll, Convert.ToInt32(start), Convert.ToInt32(limit));
}


在Ext4的Ext.data.DirectStore中取消sortInfo和paramOrder属性,改为继承自Ext.util.MixedCollection的sorters。但是无论是官方example还是direct的example,都没有相应的说明和例子,那首先按文档把js写好:

var store = Ext.create('Ext.data.DirectStore', {
autoLoad: true,
autoSave: true,
remoteSort: true,
api: {
create: MyApp.FileAction.Create,
read: MyApp.FileAction.Load,
update: MyApp.FileAction.Update,
destroy: MyApp.FileAction.Destroy
},
writer: new Ext.data.JsonWriter({
encode: false,
writeAllFields: true
}),
idProperty: 'id',
totalProperty: 'total',
root: 'data',
sorters: [{
property: 'datetimeCreated',
direction: 'DESC'
}],
fields: ['title', 'classId',{
name: 'datetimeCreated',
mapping: 'datetimeCreated',
type: 'date',
dateFormat: 'timestamp'
}, 'docAbstract','typeId']
});


注意里面的sorters是MixedCollection类型,可以写多个(用于支持多排序)

测试通过,跟踪一下firebug看看,post过去的是这个:



貌似也是MixedCollection类型,后台通过Dictionary<string, object>接受,最终调试成功的代码为:

[DirectMethod]
public FileInfoSerializer Load(Dictionary<string,object> arg)
{
object[] sorters = (object[])arg["sort"];
Dictionary<string, object> sorter = (Dictionary<string, object>)sorters[0];
FileInfoCollection coll = new FileInfoCollection();
coll.OrderField = sorter["property"].ToString();
coll.OrderDirection = sorter["direction"].ToString().ToLower() == "desc" ? FileInfoCollection.Direction.DESC : FileInfoCollection.Direction.ASC;
return new FileInfoSerializer(coll);
}


可以通过object[]数组提取各个排序信息,也可以提取start、page、limit参数用于分页。

继续研究Ext4,变化真的不小。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: