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 specifying data transform via custom JS function #536

Closed
LeaVerou opened this issue Jun 25, 2019 · 6 comments
Closed

Allow specifying data transform via custom JS function #536

LeaVerou opened this issue Jun 25, 2019 · 6 comments
Milestone

Comments

@LeaVerou
Copy link
Member

LeaVerou commented Jun 25, 2019

Idea inspired by @plehegar's markup-based system.

While we do generally try to avoid any need for custom JS, sometimes custom things are needed. We do have mv-path for simple property-chain transformations and property lookups, which has the benefit of being automatically reversible, but that doesn't cover all possible transformations, and even for good JS developers, it's kinda tricky right now to write a custom data transformation.
We should allow the user to specify custom transformations (in/out), either in MavoScript or JS, and possibly even make the current ones a special case of those (though that would be backwards incompatible, but hey, we're still in v0.x.y).

@LeaVerou LeaVerou added this to the v0.3.0 milestone Jun 25, 2019
LeaVerou added a commit that referenced this issue Jun 11, 2020
@LeaVerou
Copy link
Member Author

LeaVerou commented Jun 11, 2020

Actually, if all we care about is transforming root data before load & before save, this can be easily done with the current hook system (I've just added another hook to facilitate). E.g.:

Mavo.hooks.add("render-start", function(env) {
	if (this.id !== "myMavoApp") {
		return;
	}

	// Transform env.data before rendering
	env.data.foo = 1;
});
Mavo.hooks.add("getdata-end", function(env) {
	if (this.id !== "myMavoApp") {
		return;
	}

	// Undo env.data transformation before saving
	delete env.data.foo;
});

So if this covers use cases, maybe this can be a documentation issue? Are there use cases for doing this on a node-by-node basis?

@LeaVerou LeaVerou closed this as completed Feb 2, 2021
@LeaVerou
Copy link
Member Author

@DmitrySharabin we should probably mention this somewhere in the docs, what do you think?

@DmitrySharabin
Copy link
Member

Totally agree! I even bookmarked this issue to be able to re-use the code snippet you suggested when needed. 😅

What if we add a new subsection to the For JS developers section? How would we name it? What about Transforming root data before load and/or save?

@LeaVerou
Copy link
Member Author

LeaVerou commented May 26, 2021

Sounds good, thank you so much! 🙏🏼

@DmitrySharabin
Copy link
Member

It’s my pleasure! Get ready for the PR. 😉

@DmitrySharabin
Copy link
Member

@LeaVerou Done! Please feel free to correct my wording.

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