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

How to pass variable to a JSON stored and loaded manifest? #7

Closed
tmikaeld opened this issue May 11, 2015 · 3 comments
Closed

How to pass variable to a JSON stored and loaded manifest? #7

tmikaeld opened this issue May 11, 2015 · 3 comments
Labels

Comments

@tmikaeld
Copy link

This worked fine when the manifest was inline, but when loaded from JSON - any previously defined variables are unreachable.

@ermouth
Copy link
Owner

ermouth commented May 11, 2015

Please provide code or better explanation what exactly you are doing and where you think you loose data.

@tmikaeld
Copy link
Author

I'm loading the manifest from JSON, it's been encoded with $.my.tojson(manifest) and it works fine, but the var itemID variable that i declare before manifest is invisible to the itemID i have declared in the manifest code. Is the manifest code running in a different namespace?

var itemID = "something";

var manifest = manifestJson;

// Init $.my
$('.editForm').my(manifest, person);

@ermouth
Copy link
Owner

ermouth commented May 12, 2015

Manifests passed as folded JSON never see local scope, only global. So they see window and all its members – but do not see local scopes.

Main consideration is safety. Since we have $.my.cache() that unfold manifest before caching, we must guarantee, that no code is executed while caching. So $.my does not use eval, it use new Function constructor, that is always executed in global scope (to be precise – without any scope).

To add local scope to manifest instance use this pattern:

  • put folded manifest to cache (it must have .id property)
  • when manifest needed, get it from cache – you‘ll receive unfolded clone – and augment/decorate it with functions, that can return all required data from local scope.

Or you can just mount your data directly to manifest before you start it.

@ermouth ermouth closed this as completed May 21, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants