官網(wǎng) Ext direct包中.NET版的問題
更新時間:2009年06月15日 17:58:03 作者:
下載了官網(wǎng)的 Ext direct 包進行研究,發(fā)現(xiàn)服務(wù)器端返回結(jié)果存在一點小問題。
主要問題在返回的結(jié)果 result 標(biāo)記對應(yīng)的數(shù)據(jù)是字符串,請看以下官方例子中返回的數(shù)據(jù):
{"type":"rpc","tid":2,"action":"Sample","method":"SaveForm","result":"{\"firstName\":\"4\",\"lastName
\":\"4\",\"age\":4}"}
“ result ”標(biāo)記對應(yīng)的是一個字符串,而不是對象,這就需要在處理數(shù)據(jù)時先要將字符串轉(zhuǎn)換成 JSON 對象才能繼續(xù)處理。這會造成使用 DirectStore 作為 Grid 數(shù)據(jù)源時取不到數(shù)據(jù)的問題。在官網(wǎng)論壇找了一下,有個例子是重寫 Ext.data.DirectProxy 的 createCallback 方法實現(xiàn)的,其目的就是在獲取到數(shù)據(jù)后,將 result 中的數(shù)據(jù)轉(zhuǎn)換為對象再返回數(shù)據(jù)。以下是重寫 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
}
}
});
例子可以到以下地址下載: http://xiazai.jb51.net/200906/yuanma/Direct.rar
不過筆者的想法是能不能在服務(wù)器端解決這個問題。在 Ext.Direct.dll 的源代碼中,筆者找到 DirectResponse 類的“ Result ”的定義類型是一個 object ,于是筆者在例子中將客戶端調(diào)用的方法的返回數(shù)據(jù)類型修改為 JObject ,但是在執(zhí)行以下語句的時候會出現(xiàn)錯誤:
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 ”屬性中的數(shù)據(jù)是“ JObject ”對象,程序就重新構(gòu)造一個 JObject 對象再組合成字符串返回,如果不是就按原方法返回。
在客戶端調(diào)用方法中只要返回一個 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;
}
復(fù)制代碼 代碼如下:
{"type":"rpc","tid":2,"action":"Sample","method":"SaveForm","result":"{\"firstName\":\"4\",\"lastName
\":\"4\",\"age\":4}"}
“ result ”標(biāo)記對應(yīng)的是一個字符串,而不是對象,這就需要在處理數(shù)據(jù)時先要將字符串轉(zhuǎn)換成 JSON 對象才能繼續(xù)處理。這會造成使用 DirectStore 作為 Grid 數(shù)據(jù)源時取不到數(shù)據(jù)的問題。在官網(wǎng)論壇找了一下,有個例子是重寫 Ext.data.DirectProxy 的 createCallback 方法實現(xiàn)的,其目的就是在獲取到數(shù)據(jù)后,將 result 中的數(shù)據(jù)轉(zhuǎn)換為對象再返回數(shù)據(jù)。以下是重寫 createCallback 方法的代碼:
復(fù)制代碼 代碼如下:
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
}
}
});
例子可以到以下地址下載: http://xiazai.jb51.net/200906/yuanma/Direct.rar
不過筆者的想法是能不能在服務(wù)器端解決這個問題。在 Ext.Direct.dll 的源代碼中,筆者找到 DirectResponse 類的“ Result ”的定義類型是一個 object ,于是筆者在例子中將客戶端調(diào)用的方法的返回數(shù)據(jù)類型修改為 JObject ,但是在執(zhí)行以下語句的時候會出現(xiàn)錯誤:
return JsonConvert .SerializeObject(response);
看來這里需要修改一下,于是筆者將 DirectProcessor 類中的以上這句修改為以下代碼:
復(fù)制代碼 代碼如下:
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 ”屬性中的數(shù)據(jù)是“ JObject ”對象,程序就重新構(gòu)造一個 JObject 對象再組合成字符串返回,如果不是就按原方法返回。
在客戶端調(diào)用方法中只要返回一個 JObject 對象就可以了,例子如下:
復(fù)制代碼 代碼如下:
[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;
}
相關(guān)文章
.NET 8 強大功能 IHostedService 與 Backgr
.NET 8 中的 IHostedService 和 BackgroundService 提供了強大的工具集,使定時任務(wù)、后臺處理以及定期維護等功能的實現(xiàn)變得更加直接、高效和靈活,感興趣的朋友跟隨小編一起看看吧2024-11-11
ASP.NET 5中使用AzureAD實現(xiàn)單點登錄
本文給大家介紹的是在ASP.NET 5中使用AzureAD實現(xiàn)單點登錄的方法和示例,有需要的小伙伴可以參考下。2015-07-07
.NET 日志系統(tǒng)設(shè)計思路及實現(xiàn)代碼
這篇文章主要介紹了.NET 日志系統(tǒng)設(shè)計思路及實現(xiàn)代碼,有需要的朋友可以參考一下2013-12-12
解析WPF實現(xiàn)音頻文件循環(huán)順序播放的解決方法
本篇文章是對WPF實現(xiàn)音頻文件循環(huán)順序播放的方法進行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
教你Asp.net下使用mysql數(shù)據(jù)庫的步驟
近日,在項目中遇到了麻煩,客戶非要求使用mysql數(shù)據(jù)庫,對于我從來么有使用過的人來說,很是頭疼,最后還是硬著頭皮弄好了。期間也遇到了各種各樣的問題,現(xiàn)在把他整理在此,希望對那些和我一樣從來沒有使用過的人,能快速入手2012-05-05

