ulion / jsonform

Build forms from JSON Schema. Easily template-able. Compatible with Twitter Bootstrap out of the box.
http://ulion.github.io/jsonform/playground/
MIT License
49 stars 27 forks source link

String to Function #12

Open R-Gerard opened 9 years ago

R-Gerard commented 9 years ago

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!');"
    }
ulion commented 9 years ago

PR is welcome.

2015-06-09 1:20 GMT+08:00 Rusty Gerard notifications@github.com:

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

R-Gerard commented 9 years ago

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?

ulion commented 9 years ago

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 notifications@github.com:

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

R-Gerard commented 9 years ago

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

ulion commented 9 years ago

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 notifications@github.com:

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