下载了官网的 Ext direct 包进行研究,发现服务器端返回结果存在一点小问题。主要问题在返回的结果 result 标记对应的数据是字符串,请看以下官方例子中返回的数据:


  
  1. {"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 方法的代码:


  
  1. Ext.override(Ext.data.DirectProxy, {  
  2. createCallback: function(action, reader, cb, scope, arg) {  
  3. return {  
  4. callback: (action == 'load') ? function(result, e) {  
  5. if (typeof result == 'string') {  
  6. result = Ext.decode(result);  
  7. }  
  8. if (!e.status) {  
  9. this.fireEvent(action + "exception", this, e, result);  
  10. cb.call(scope, null, arg, false);  
  11. return;  
  12. }  
  13. var records;  
  14. try {  
  15. records = reader.readRecords(result);  
  16. }  
  17. catch (ex) {  
  18. this.fireEvent(action + "exception", this, e, result, ex);  
  19. cb.call(scope, null, arg, false);  
  20. return;  
  21. }  
  22. this.fireEvent(action, this, e, arg);  
  23. cb.call(scope, records, arg, true);  
  24. } : function(result, e) {  
  25. if (typeof result == 'string') {  
  26. result = Ext.decode(result);  
  27. }  
  28. if (!e.status) {  
  29. this.fireEvent(action + "exception", this, e);  
  30. cb.call(scope, null, e, false);  
  31. return;  
  32. }  
  33. this.fireEvent(action, this, result, e, arg);  
  34. cb.call(scope, result, e, true);  
  35.  
  36. },  
  37. scope: this  
  38. }  
  39. }  
  40. }); 

例子可以到以下地址下载: http://ny504427.bizhostnet.com/Default.aspx

不过笔者的想法是能不能在服务器端解决这个问题。在 Ext.Direct.dll 的源代码中,笔者找到 DirectResponse 类的“ Result ”的定义类型是一个 object ,于是笔者在例子中将客户端调用的方法的返回数据类型修改为 JObject ,但是在执行以下语句的时候会出现错误:

  
  1. return JsonConvert .SerializeObject(response);  

看来这里需要修改一下,于是笔者将 DirectProcessor 类中的以上这句修改为以下代码:


  
  1. JObject o = new JObject (   
  2.  
  3.                             new JProperty ("type" ,response.Type),   
  4.  
  5.                             new JProperty ("tid" ,response.TransactionId),   
  6.  
  7.                             new JProperty ("action" ,response.Action),   
  8.  
  9.                              new JProperty ("method" ,response.Method),   
  10.  
  11.                             new JProperty ("result" ,(JObject )response.Result)   
  12.  
  13.                         );   
  14.  
  15.                         return o.ToString();  

其作用就是如果“ Result ”属性中的数据是“ JObject ”对象,程序就重新构造一个 JObject 对象再组合成字符串返回,如果不是就按原方法返回。

在客户端调用方法中只要返回一个 JObject 对象就可以了,例子如下:

  
  1. [DirectMethod ]   
  2.  
  3.         public object GetGridDatas()   
  4.  
  5.         {   
  6.  
  7.             JArray ja = new JArray ();   
  8.  
  9.             for (int i = 0; i < 2; i++)   
  10.  
  11.             {   
  12.  
  13.                 ja.Add(new JObject (   
  14.  
  15.                     new JProperty ("id" ,i),   
  16.  
  17.                     new JProperty ("title" ,"title" +i.ToString())   
  18.  
  19.                 ));   
  20.  
  21.             }   
  22.  
  23.             JObject o = new JObject (   
  24.  
  25.                 new JProperty ("results" , 2),   
  26.  
  27.                 new JProperty ("rows" , ja)   
  28.  
  29.             );   
  30.  
  31.             return o;   
  32.  
  33.         }