2012-03-21 58 views
1

通过JSON元数据重新配置ExtJS,发现我需要提供一些JavaScript函数作为重新配置列的一部分。JSON响应是否没有“字符串”值?

如果函数被封装在引号中,函数不会被解释为函数,那么是否有可能返回带有未引用标记值的JSON?

理想我想有这样的事情

{"d":{ 
    "metaData": { 
     "root": "d.data", 
     "fields": [{ 
      "type": "date", 
      "name": "Date", 
      -->"renderer": formatDate, 
      "dateFormat": "c", 
      "convert": function (newValue, model) { 
        return Ext.Date.parse(newValue, "MS");<-- 
       }, 
      "header": "Date", 
      "dataIndex": "Date" 
     }, { 
      "type": "string", 
      "name": "Notes", 
      "header": "Notes", 
      "dataIndex": "Notes" 
     }, {... 

我还与C#的工作,所以我将返回JSON作为Dictionary<string,object>

+0

它不能。这似乎与http://stackoverflow.com/questions/2001449/is-it-valid-to-define-functions-in-json-results – 2012-03-21 11:44:09

+0

重复啊,这很可悲。没问题,但我刚刚输出日期为字符串!你可以把你的评论作为答案,所以我可以标记:) – MHTri 2012-03-21 13:00:59

回答

0

严格地说,Json不能有功能。但是,您可以将其存储为一个字符串,然后“EVAL”它

... 
"convert": "function (newValue, model) { return Ext.Date.parse(newValue, 'MS'); }", //As a string 
... 

然后,你这样做是为了执行它(假设将字符串函数在convert VAR):

var func = eval('(' + convert +')'); 
alert(func(3, 5)); //Just call it as a normal function now. 

希望能帮助到你。 干杯。

+0

嗨,谢谢你。你有没有使用ExtJS的经验,我不确定何时/何时运行评估函数。 – MHTri 2012-04-05 14:33:09