1

Working on reconfiguring ExtJS via JSON metadata, found out that I need to provide some javascript functions as part of configuring the columns again.

The functions don't get interpreted as functions if they're encased in quote marks, so is it possible to return a JSON with un-quotemarked values?

Ideally I would have something like

{"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"
        }, {...

I'm also working with C#, so would I return the JSON as a Dictionary<string,object>?

MHTri
  • 910
  • 2
  • 10
  • 30
  • It can't. It seems duplicate with http://stackoverflow.com/questions/2001449/is-it-valid-to-define-functions-in-json-results – Chris Li Mar 21 '12 at 11:44
  • Ah, that's sad. No problem though, I've just outputted the dates as strings! Could you put your comment as an answer so I can mark it :) – MHTri Mar 21 '12 at 13:00

1 Answers1

0

Strictly talking, Json can't have functions. However, you can store it as a string and then 'eval' it

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

Then you'd do this to execute it (assuming the stringified function is in the convert var):

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

Hope it helps. Cheers.

Edgar Villegas Alvarado
  • 18,204
  • 2
  • 42
  • 61
  • Hi, thanks for this. Have you any experience with ExtJS, I'm not sure where/when to run the evaluated function. – MHTri Apr 05 '12 at 14:33