-
Notifications
You must be signed in to change notification settings - Fork 59
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
Implement "prepareStateForRender" — Fixes #178 #181
Implement "prepareStateForRender" — Fixes #178 #181
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot @yuchi, this will be very useful :D
I've only added one inline comment, and besides that can you just rename your commits so that they follow the format we use? We basically write commit names as if they've started with the word If
, so in your case they'd be something like: Applies source formatting
, Lets ...
and Add tests for "prepareStateForRender"
.
@@ -111,6 +112,27 @@ describe('Soy', function() { | |||
done(); | |||
}); | |||
}); | |||
|
|||
it('should pass state values to "getTemplateData" and use them in the template', function(done) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This still uses the old getTemplateData
name :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
Sorry I'm used to |
While in JSXComponents there’s a place to compute derived values from the state just before rendering (that’s the "render" method), on Soy components there’s no way to do the same. This change makes the Soy renderer to call a "prepareStateForRender" method on the component, passing the data that would be used for the template and trusting the implementation to do one of the following: - mutate the original template data and return it; - mutate the original template data and return undefined; - create a new object and return it (eventually mixing in the original data). Fixes metal#178
70c4cd8
to
970e6a6
Compare
@@ -60,7 +62,12 @@ class Soy extends IncrementalDomRenderer.constructor { | |||
data[params[i]] = component[params[i]].bind(component); | |||
} | |||
} | |||
return data; | |||
|
|||
if (isFunction(component.prepareStateForRender)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mairatma Didn’t want to make data
mutable, so I’m branching the return here. Tell me if this is ok with you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, that's fine.
@@ -11,6 +11,7 @@ import { Events as EventsComponent } from './assets/Events.soy.js'; | |||
// on it. | |||
// TODO: We should have a better dependency management for soy files so that | |||
// the order in which they're required doesn't matter. | |||
import { ComputedData as ComputedDataComponent } from './assets/ComputedData.soy.js'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you make sure this is the right line to place the import
? I simply sorted them alphabetically.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's correct, I've just realized that this TODO isn't necessary anymore, all imports can already be in alphabetical order today :)
@@ -111,6 +112,27 @@ describe('Soy', function() { | |||
done(); | |||
}); | |||
}); | |||
|
|||
it('should pass state values to "getTemplateData" and use them in the template', function(done) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
Just started reviewing :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot, merging now!
@@ -11,6 +11,7 @@ import { Events as EventsComponent } from './assets/Events.soy.js'; | |||
// on it. | |||
// TODO: We should have a better dependency management for soy files so that | |||
// the order in which they're required doesn't matter. | |||
import { ComputedData as ComputedDataComponent } from './assets/ComputedData.soy.js'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's correct, I've just realized that this TODO isn't necessary anymore, all imports can already be in alphabetical order today :)
@@ -60,7 +62,12 @@ class Soy extends IncrementalDomRenderer.constructor { | |||
data[params[i]] = component[params[i]].bind(component); | |||
} | |||
} | |||
return data; | |||
|
|||
if (isFunction(component.prepareStateForRender)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, that's fine.
If you could it would be great! |
Of course, I can. For me, it seems a Lifecycle method. Isn't it? |
It
|
Thank you, @yuchi. I'm going to write it. 😃 |
Let Soy components define "prepareStateForRender" to manipulate data:
While in JSXComponents there’s a place to compute derived values from the state just before rendering (that’s the "render" method), on Soy components there’s no way to do the same.
This change makes the Soy renderer to call a "prepareStateForRender" method on the component, passing the data that would be used for the template and trusting the implementation to do one of the following:
Fixes #178