Skip to content

Alloy lazy transformation pattern #13574

Answered by m1ga
beppo-ivel asked this question in Q&A
Discussion options

You must be logged in to vote

only tested it with my model that doesn't have dates so I've used a simple string:

transform: function() {
	var model = this;
	var t = this.toJSON();

	Object.defineProperty(t, 'dateFormatted', {
		get: function() {
			return "test";
		}
	});

	return t;
}
	var book = Alloy.createModel('library', {
		title: 'Title' + Math.floor(Math.random() * 1000),
		subtitle: 'asdfasdf ' + Math.floor(Math.random() * 1000),
		image: 'https://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/SIPI_Jelly_Beans_4.1.07.tiff/lossy-page1-220px-SIPI_Jelly_Beans_4.1.07.tiff.jpg'
	});
	console.log(book.transform());
	console.log(book.transform().dateFormatted);

and it will log

[INFO]  {
[INFO]  title: 'Title940',…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@beppo-ivel
Comment options

Answer selected by beppo-ivel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants