Do I need to restructure my Backbone models/collections? Unsure how to save all at once
My application revolves around people. I have a Person model where you can set the name, a开发者_如何学JAVAge, and other traits. On the same page I have a Collection of Interest models that can be added for the person.
I need to pass a JSON representation of the Person and their Interests to the server. But I do not know how to bring these pieces together (the Person and Collection of Interests are separate).
Do I need to restructure my application? Should the Interests be a property of the People model? Should it still be a collection or should I change it to an array of native Javascript objects?
First, it sounds like Person should have a collection of Interest models. You should be able to see an example of this on the backbone.js documentation page -- see the Mailbox example. Link here: http://documentcloud.github.com/backbone/#FAQ-nested
However, if you are trying to have one single call to update your Person and Interests, you'll more than likely have to overload the save method to save the model and the collection too. To do that, it seems like you might have to temporarily add Interest data to the attributes of the Person model. Then when you do a save, these nested attributes go to the server. This is a hack, though.
精彩评论