-
Notifications
You must be signed in to change notification settings - Fork 75
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
Force child states to parse on initial data #190
Conversation
fixes #146 I ran into this issue today, and would like to restart the conversation Open questions: - should this be the default behavior for both children and collections? - if not, does it make sense to add an option somewhere?
I think parsing collections/children is dangerous, since some people use enveloping (that's what parse is for). This might cause nested objects to be mis-parsed. |
I don't disagree @pgilad, especially when it comes to collections, but wanted to get the discussion restarted with a PR with passing tests. Would it make sense to have an option on ampersand-state to set whether or not to parse children/collections? Something like this?
For my use-case, I'm parsing json-api formatted data where all of my state/model data comes in the following format:
I only want the collection to parse once (to remove the The discrepancy is that in this setup: var Employee = AmpersandState.extend({
props: {...},
collections: {
teams: Teams
},
children: {
position: Position
}
}); the individual models in the Teams collection trigger |
I think parse should almost never be (automatically) triggered for nested children/collections. It could be set manually (i.e {
data: [
{
"id": "1",
"attributes": {
"name": "Bob Holt",
"rank": "pvt",
"serial": 12345
}
},
{
"id": "2",
"attributes": {
"name": "Jean Valjean",
"rank": "pvt",
"serial": 24601
}
}
]
} Even in your example, if {
data: [
{
"id": "1",
"attributes": {
"data: {
"name": "Bob Holt",
"rank": "pvt",
"serial": 12345
}
}
},
]
} |
Thanks, @pgilad. I'll update the PR later today with these as optional. You're right, no server would serve data that way, but I think you're mixing up the parsing from the collection and from the model and what happens when models that need parsing are nested. The collection's parse just removes the data property and returns the array. This only happens once when data is loaded, and is the collection's parse method:
Cool. But all models need to be flattened every time:
needs to be:
The nested models in the response retain the
In current ampersand-state, the models in This is a reiteration of the problem described and captured in a full test-case in #146. |
Hey @bobholt, so I'm totally on board with getting this fixed, but I think @HenrikJoreteg's original tests are lacking somewhat, such that while this PR will pass the tests, it's not actually going to work in practice. The real question isn't really is parse called at all, but, is parse called at the time when we set the data we want to be parsed. Really the tests should be testing that parse received the data sent to add (e.g. for child Currently in this PR, we're just going to call parse when we initialize the empty objects, which isn't going to do a whole lot. We're going to need to parse on set for the child data, which occurs here: https://github.com/AmpersandJS/ampersand-state/blob/master/ampersand-state.js#L165-L167 as called in the constructor here: https://github.com/AmpersandJS/ampersand-state/blob/master/ampersand-state.js#L43 Which ultimately starts to look a lot like: #109 and I believe if that was merged it would fix this issue too, but as per the discussion there appears to be a bug with double parsing in #109 :/ |
I also ran into a slightly related issue with #90, where if you want to get events (such as Just adding this as additional information in case someone runs into that same thing, not saying that it needs to be fixed as part of this PR. |
Please see my response on #146 That said - I'm against parsing as default when initializing non-server data. |
fixes #146
I ran into this issue today, and would like to restart the conversation.
This fixes the failing test @HenrikJoreteg created, but some open questions remain: