2012-03-21 28 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