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
Give model.set a parse option #2627
Comments
I'd defer to @caseywebdev about the do-ability of this, but I think its certainly reasonable to have API parity between models and collections parsing. Thanks for opening an issue @etler! |
Implementation should be pretty straight forward. I think the major question is if there's any reason not to? Also, what would happen when a key val was provided instead of an attribute object? |
This sounds good, but the case that will cause a snag at the top of my head is |
Great, I'd love to take a stab at it. |
I was actually going to open a ticket for this as well - but then came across #2013... though I'd assume that was before there was a I agree with @wookiehangover that the API parity would be nice to have, and I've come across cases where it makes sense when using parse to handle nested data (I've just been using |
@tgriesser I don't know how I missed that issue! I agree with that discussion for that use case, but I think a more compelling use case is for parsing bootstrapped data (as well as nested parsing, which I'm doing also). I've also been using the same workaround, but as you said, now that there's a |
What's the actual use case for needing this? |
If you want to call class Book extends Models.Document
constructor: ->
@info = new InfoModel()
@chapters = new ChaptersCollection()
super
parse: (attrs, options) ->
@info.set(attrs.info, options)
@chapters.set(attrs.chapters, options)
_.omit(attrs, 'info', 'chapters') Now you can do this: book.set({
title: 'title',
info: {
some: data
someNested: {
otherData: ...
}
},
chapters: [{...}, {...}]
}, {parse: true}); and it should work... (yes, |
Anyone have a thought about how this change would work with defaults? And without double-parsing? |
To get it to work with defaults, I created an |
Let's move this conversation over to the open PR... |
Is there any update on this? |
I'm actually using model.set(model.parse(data)) right now since the parse option is missing. This would be really nice to have for consistency with collection.reset etc. |
The collection counterpart of set allows you to provide a "parse" option to run the parser function before processing the collection object. This is handy when you have bootstrap data that you want to build a collection from, as it wont go through the fetch function.
Is there a reason the model object doesn't provide the same option to parallel collection? It seems inconsistent to me that it is omitted.
Edit: Upon further inspection, reset does still respects the parse option.
The text was updated successfully, but these errors were encountered: