AmpersandJS / ampersand-model

Observable objects, for managing state in applications.
MIT License
84 stars 31 forks source link

Automatically setting collection and collection items parent for models initialized from JSON #48

Closed kfrajtak closed 9 years ago

kfrajtak commented 9 years ago

Hi, it would be nice to automatically setting collection and collection items parent for models initialized from JSON data. I have a model A that has a collection of items B and I'd like to access a property of the model A in a view rendered for item B. Or maybe there's a way how to achieve this and if anybody can tell me how, that would be nice :) Thanks, Karel

wraithgar commented 9 years ago

I believe itemB.collection.parent should get you to model A. Lemme know if that works.

kfrajtak commented 9 years ago

Sorry for getting back to you quite late. Yes, that works, but what is the real difference between itemB.parent and itemB.collection.parent? I thought the first one is cleaner (but I might be wrong). Thanks for your reply anyway.

wraithgar commented 9 years ago

If a model is part of a collection then it will have that collection in its collection attribute. The model's parent attribute is for when a model has other models as children, outside of a collection setting.