Skip to content
This repository has been archived by the owner on Apr 22, 2021. It is now read-only.

Knockback compatibility #34

Open
neverfox opened this issue Jun 13, 2013 · 5 comments
Open

Knockback compatibility #34

neverfox opened this issue Jun 13, 2013 · 5 comments

Comments

@neverfox
Copy link

First of all, supermodel is a really elegant library. I was hoping it would help take the place of backbone-relational in a knockback app I'm working on. However, because "nested data is used and removed with model.parse," it creates some difficulties. That's unfortunate when other libraries assume those nested collections will be attributes but perhaps that's the only way to achieve what supermodel is trying to achieve and there's no way around it.

Knockback does apparently have the ability to get pretty fancy with customizing how things get constructed in a kb.ViewModel so perhaps a workaround is possible. Does anyone have any experience making these libraries work together?

@neverfox
Copy link
Author

OK, I finally figured out what "Nested data can be provided through the source property in a model's attributes" means in practice. It restores the attribute and clears the collection returned by the association function. But what does that mean exactly? Does that mean that by using source, I'm essentially turning off the association? It's not clear to me when I should or shouldn't be using source.

@braddunbar
Copy link
Contributor

Hi @neverfox! The source option only needs to be used when the association name differs from the JSON property it should be retrieved from. Otherwise they should be the same.

@neverfox
Copy link
Author

Hi @braddunbar. I see. Thanks. I had a long set of questions here before but, after playing around, I think I see how it all works now. What I wasn't understanding before was that JSON for a List model like {"id":1,"items":[{"id":1},{"id":2}]} gets restructured like {id: 1} (a List) and then separately Items {id:1, list_id: 1} and {id: 2, list_id: 1}. What I thought it was going to be was a List model that stayed in the basic format of the original JSON with an embedded Items collection. This was the source of much confusion but I'm clear now. I'm still now sure what that might mean for use with Knockback yet. I think backbone-relational takes the latter approach to building up its associations and this is what Knockback uses in its examples of relational models. I'm going to see how far I can get with supermodel as a replacement.

@neverfox
Copy link
Author

So what do you typically do when you want to save models back to the server? Because the JSON gets pulled apart by supermodel, save-ing a list would just send {id: 1} back.

@johanneslumpe
Copy link

@neverfox you could overwrite the toJSON method and then add a serialized version of your associated models back in.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants