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

Allow context.variable.function in a component context. #697

Open
samuelbirch opened this issue Dec 16, 2020 · 3 comments
Open

Allow context.variable.function in a component context. #697

samuelbirch opened this issue Dec 16, 2020 · 3 comments

Comments

@samuelbirch
Copy link

Can it be supported that functions will work in the component context object.

module.exports = { context: { comeVariableName: { someFunction: function(param){ return "value"; } } } }

it can work in the same way as normal variables in terms of rendering the value.
Its so we can use the syntax in twig.

@mihkeleidast
Copy link
Member

The context is parsed into a JSON object and functions can not be represented in JSON as far as I know.

I investigated this in #539 a bit (since I would love to have this for React projects) but concluded that this is probably not going to happen because of the JSON requirement.

If you have any ideas on how to implement this without a breaking change, I'm all ears.

@samuelbirch
Copy link
Author

samuelbirch commented Dec 16, 2020

would something like this work?

JSON.stringify( { a: 5 , b: x => x }, (k,v) => typeof v === "function" ? "" + v : v);

JSON.parse(json, (k,v) => typeof v === "string"? (v.startsWith('function')? eval("("+v+")") : v): v)

from: https://stackoverflow.com/questions/36517173/how-to-store-a-javascript-function-in-json

@mihkeleidast
Copy link
Member

Not really - that technique is a bit hacky in the sense that it's basically a workaround for what JSON does not support. Additionally, the evaling of the string to be an actual function again is basically a security exploit waiting to happen and I would not allow this in Fractal source.

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

No branches or pull requests

2 participants