Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String to Function #12

Open
R-Gerard opened this issue Jun 8, 2015 · 5 comments
Open

String to Function #12

R-Gerard opened this issue Jun 8, 2015 · 5 comments

Comments

@R-Gerard
Copy link

R-Gerard commented Jun 8, 2015

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
Copy link
Owner

ulion commented Jun 8, 2015

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
#12.

Ulion

@R-Gerard
Copy link
Author

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
Copy link
Owner

ulion commented Jun 16, 2015

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
#12 (comment).

Ulion

@R-Gerard
Copy link
Author

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
Copy link
Owner

ulion commented Jun 17, 2015

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
#12 (comment).

Ulion

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants