当前位置: 移动技术网 > IT编程>开发语言>.net > 官网 Ext direct包中.NET版的问题

官网 Ext direct包中.NET版的问题

2017年12月12日  | 移动技术网IT编程  | 我要评论

2013经典语录,贵州黄果树旅游报价,ak1521航班

主要问题在返回的结果 result 标记对应的数据是字符串,请看以下官方例子中返回的数据:
复制代码 代码如下:

{"type":"rpc","tid":2,"action":"sample","method":"saveform","result":"{\"firstname\":\"4\",\"lastname
\":\"4\",\"age\":4}"}

“ result ”标记对应的是一个字符串,而不是对象,这就需要在处理数据时先要将字符串转换成 json 对象才能继续处理。这会造成使用 directstore 作为 grid 数据源时取不到数据的问题。在官网论坛找了一下,有个例子是重写 ext.data.directproxy 的 createcallback 方法实现的,其目的就是在获取到数据后,将 result 中的数据转换为对象再返回数据。以下是重写 createcallback 方法的代码:
复制代码 代码如下:

ext.override(ext.data.directproxy, {
createcallback: function(action, reader, cb, scope, arg) {
return {
callback: (action == 'load') ? function(result, e) {
if (typeof result == 'string') {
result = ext.decode(result);
}
if (!e.status) {
this.fireevent(action + "exception", this, e, result);
cb.call(scope, null, arg, false);
return;
}
var records;
try {
records = reader.readrecords(result);
}
catch (ex) {
this.fireevent(action + "exception", this, e, result, ex);
cb.call(scope, null, arg, false);
return;
}
this.fireevent(action, this, e, arg);
cb.call(scope, records, arg, true);
} : function(result, e) {
if (typeof result == 'string') {
result = ext.decode(result);
}
if (!e.status) {
this.fireevent(action + "exception", this, e);
cb.call(scope, null, e, false);
return;
}
this.fireevent(action, this, result, e, arg);
cb.call(scope, result, e, true);
},
scope: this
}
}
});

例子可以到以下地址下载:
不过笔者的想法是能不能在服务器端解决这个问题。在 ext.direct.dll 的源代码中,笔者找到 directresponse 类的“ result ”的定义类型是一个 object ,于是笔者在例子中将客户端调用的方法的返回数据类型修改为 jobject ,但是在执行以下语句的时候会出现错误:
return jsonconvert .serializeobject(response);
看来这里需要修改一下,于是笔者将 directprocessor 类中的以上这句修改为以下代码:
复制代码 代码如下:

if (response.result.gettype().tostring() == "newtonsoft.json.linq.jobject" )
{
jobject o = new jobject (
new jproperty ("type" ,response.type),
new jproperty ("tid" ,response.transactionid),
new jproperty ("action" ,response.action),
new jproperty ("method" ,response.method),
new jproperty ("result" ,(jobject )response.result)
);
return o.tostring();
}
else
{
return jsonconvert .serializeobject(response);
}

其作用就是如果“ result ”属性中的数据是“ jobject ”对象,程序就重新构造一个 jobject 对象再组合成字符串返回,如果不是就按原方法返回。
在客户端调用方法中只要返回一个 jobject 对象就可以了,例子如下:
复制代码 代码如下:

[directmethod ]
public object getgriddatas()
{
jarray ja = new jarray ();
for (int i = 0; i < 2; i++)
{
ja.add(new jobject (
new jproperty ("id" ,i),
new jproperty ("title" ,"title" +i.tostring())
));
}
jobject o = new jobject (
new jproperty ("results" , 2),
new jproperty ("rows" , ja)
);
return o;
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网