I have a form for editing a provider record. The provider record has a relationship with multiple address, so a provider can has home, office, vacation, etc address types.
The json addresses object that comes from server just brings the addresses that the provider has, so if the provider has only a “vacation” type address then that’s the only record that comes in but in the editing form I must have to have all the addresses types in order to edit the existing ones or add a new one.
I don’t know if my approach is fine but I have in my form a “jsonBaseAddresses” that contains all the address possible types and this object is binded using Knockout to the form (this actually works) so I want to merge the “jsonRemoteAddress” with “jsonBaseAddresses” in order to have the “jsonBaseAddresses” but merged with the data that comes in the remote json object.
Again, i dont know if this is the right way to do it. Appreciate any help. Here is a jsFiddle sample of what i want to do.
Thanks
Why don’t you just iterate over the remote addresses and push them to addresses in the view model like ?
And so on …
To merge addresses you can use following solutions:
You can introduce property to Address like Equals and compare fields
that identify the unique address
Select any implementation in this post How to determine equality for two JavaScript objects?
to get the address hash code and in the loop above compare the hash
codes of the addresses to merge. I used this solution in one of my
projects. And also check this http://documentcloud.github.com/underscore/#isEqual