官網 Ext direct包中.NET版的問題
更新時間:2009年06月15日 17:58:03 作者:
下載了官網的 Ext direct 包進行研究,發(fā)現(xiàn)服務器端返回結果存在一點小問題。
主要問題在返回的結果 result 標記對應的數(shù)據(jù)是字符串,請看以下官方例子中返回的數(shù)據(jù):
{"type":"rpc","tid":2,"action":"Sample","method":"SaveForm","result":"{\"firstName\":\"4\",\"lastName
\":\"4\",\"age\":4}"}
“ result ”標記對應的是一個字符串,而不是對象,這就需要在處理數(shù)據(jù)時先要將字符串轉換成 JSON 對象才能繼續(xù)處理。這會造成使用 DirectStore 作為 Grid 數(shù)據(jù)源時取不到數(shù)據(jù)的問題。在官網論壇找了一下,有個例子是重寫 Ext.data.DirectProxy 的 createCallback 方法實現(xiàn)的,其目的就是在獲取到數(shù)據(jù)后,將 result 中的數(shù)據(jù)轉換為對象再返回數(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
不過筆者的想法是能不能在服務器端解決這個問題。在 Ext.Direct.dll 的源代碼中,筆者找到 DirectResponse 類的“ Result ”的定義類型是一個 object ,于是筆者在例子中將客戶端調用的方法的返回數(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 ”對象,程序就重新構造一個 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;
}
復制代碼 代碼如下:
{"type":"rpc","tid":2,"action":"Sample","method":"SaveForm","result":"{\"firstName\":\"4\",\"lastName
\":\"4\",\"age\":4}"}
“ result ”標記對應的是一個字符串,而不是對象,這就需要在處理數(shù)據(jù)時先要將字符串轉換成 JSON 對象才能繼續(xù)處理。這會造成使用 DirectStore 作為 Grid 數(shù)據(jù)源時取不到數(shù)據(jù)的問題。在官網論壇找了一下,有個例子是重寫 Ext.data.DirectProxy 的 createCallback 方法實現(xiàn)的,其目的就是在獲取到數(shù)據(jù)后,將 result 中的數(shù)據(jù)轉換為對象再返回數(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
不過筆者的想法是能不能在服務器端解決這個問題。在 Ext.Direct.dll 的源代碼中,筆者找到 DirectResponse 類的“ Result ”的定義類型是一個 object ,于是筆者在例子中將客戶端調用的方法的返回數(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 ”對象,程序就重新構造一個 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;
}
相關文章
ASP.NET Core應用錯誤處理之三種呈現(xiàn)錯誤頁面的方式
這篇文章主要給大家介紹了關于ASP.NET Core應用錯誤處理之三種呈現(xiàn)錯誤頁面的方式的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-01-01運用.net core中實例講解RabbitMQ高可用集群構建
這篇文章主要介紹了運用.net core中實例講解RabbitMQ高可用集群構建,文中相關示例代碼講解的非常清晰,感興趣的小伙伴可以參考一下這篇文章,相信可以幫助到你2021-09-09c# 操作符?? null coalescing operator
?? "null coalescing" operator 是c#新提供的一個操作符,這個操作符提供的功能是判斷左側的操作數(shù)是否是null,如果是則返回結果是右側的操作數(shù);非null則返回左側的操作數(shù)。2009-06-06