jsonform icon indicating copy to clipboard operation
jsonform copied to clipboard

String to Function

Open R-Gerard opened this issue 9 years ago • 5 comments

The examples for event handlers will not pass through a strict JSON parser, which makes it impossible to store the schema as pure json (e.g. external to the app, or to be passed around between components).

http://ulion.github.io/jsonform/playground/?example=events

A "string to function" feature would be extremely useful for this. For example, instead of:

    {
      "type": "button",
      "title": "Click me",
      "onClick": function (evt) {
        evt.preventDefault();
        alert('Thank you!');        
      }
    }

...we could write:

    {
      "type": "button",
      "title": "Click me",
      "onClick": "function (evt) { evt.preventDefault(); alert('Thank you!'); }"
    }

...or even just statements that could be wrapped in an eval call, instead of a full-fledged function:

    {
      "type": "button",
      "title": "Click me",
      "onClick": "evt.preventDefault(); alert('Thank you!');"
    }

R-Gerard avatar Jun 08 '15 17:06 R-Gerard

PR is welcome.

2015-06-09 1:20 GMT+08:00 Rusty Gerard [email protected]:

The examples for event handlers will not pass through a strict JSON parser, which makes it impossible to store the schema as pure json (e.g. external to the app, or to be passed around between components).

http://ulion.github.io/jsonform/playground/?example=events

A "string to function" feature would be extremely useful for this. For example, instead of:

{
  "type": "button",
  "title": "Click me",
  "onClick": function (evt) {
    evt.preventDefault();
    alert('Thank you!');
  }
}

...we could write:

{
  "type": "button",
  "title": "Click me",
  "onClick": "function (evt) { evt.preventDefault(); alert('Thank you!'); }"
}

...or even just statements that could be wrapped in an eval call, instead of a full-fledged function:

{
  "type": "button",
  "title": "Click me",
  "onClick": "evt.preventDefault(); alert('Thank you!');"
}

— Reply to this email directly or view it on GitHub https://github.com/ulion/jsonform/issues/12.

Ulion

ulion avatar Jun 08 '15 22:06 ulion

My workaround is to use a reviver before initializing the JSON Form:

// Reviver method for JSON.parse to coerce strings to other objects
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Example:_Using_the_reviver_parameter
function preprocessJson(k, v) {
  // Convert strings to functions
  if (typeof v === "string" && v.startsWith("function")) {
    console.log("Reviving function: " + v);
    eval("var f = " + v);
    return f;
  }

  return v;
}
var myForm = JSON.parse('{...}', preprocessJson);
$('form').jsonForm(myForm);

If this is useful I can update the wiki. Thoughts?

R-Gerard avatar Jun 16 '15 21:06 R-Gerard

It's cool, please do that. and maybe also the original repo's wiki please.

2015-06-17 5:12 GMT+08:00 Rusty Gerard [email protected]:

My workaround is to use a reviver before initializing the JSON Form:

// Reviver method for JSON.parse to coerce strings to other objects// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Example:_Using_the_reviver_parameterfunction preprocessJson(k, v) { // Convert strings to functions if (typeof v === "string" && v.startsWith("function")) { console.log("Reviving function: " + v); eval("var f = " + v); return f; }

return v; }

var myForm = JSON.parse('{...}', preprocessJson); $('form').jsonForm(myForm);

If this is useful I can update the wiki. Thoughts?

— Reply to this email directly or view it on GitHub https://github.com/ulion/jsonform/issues/12#issuecomment-112571065.

Ulion

ulion avatar Jun 16 '15 21:06 ulion

Sorry, which wiki are you referring to? I'm only aware of https://github.com/joshfire/jsonform/wiki There's no content at https://github.com/ulion/jsonform/wiki

R-Gerard avatar Jun 17 '15 21:06 R-Gerard

Ok, it's cool, I thought I had one, never mind. use the one which peoples know.

2015-06-18 5:27 GMT+08:00 Rusty Gerard [email protected]:

Sorry, which wiki are you referring to? I'm only aware of https://github.com/joshfire/jsonform/wiki There's no content at https://github.com/ulion/jsonform/wiki

— Reply to this email directly or view it on GitHub https://github.com/ulion/jsonform/issues/12#issuecomment-112954315.

Ulion

ulion avatar Jun 17 '15 21:06 ulion