0

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.

http://jsfiddle.net/HJrGf/4/

Thanks

VAAA
  • 14,531
  • 28
  • 130
  • 253
  • To clarify, the list of base addresses is the only *possible* set of options? If the server sends over a type not in that list, should it be ignored? – Kyeotic Jul 12 '12 at 17:36
  • Yes, thats a fixed list. No more options are allowed in the database. – VAAA Jul 12 '12 at 17:40

1 Answers1

1

Why don't you just iterate over the remote addresses and push them to addresses in the view model like ?

var vm = new App(jsonBaseAddresses );
for ( var i = 0; i < jsonRemoteAddresses.length; i++){
vm.addresses.push(new Address(jsonRemoteAddresses[i]);
}

And so on ...

To merge addresses you can use following solutions:

  1. You can introduce property to Address like Equals and compare fields that identify the unique address

  2. 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

Community
  • 1
  • 1
Madman
  • 3,171
  • 2
  • 32
  • 44
  • Thanks Madman, If I push then I will get more address and there are only 4 types of addresses available. So what i need is like to merge them so I always have 4 types. – VAAA Jul 12 '12 at 18:08
  • mmm yeah maybe doing a loop base addressses and see if the address exits in remote addresses, if so then loop trough all properties and update them using the remote address right? – VAAA Jul 12 '12 at 18:21
  • I think you don't need to loop over all properties, just those that make address class unique. In any case I think we on the same page – Madman Jul 12 '12 at 18:26
  • the other solution i was thinking is to create the same object on the server... going to be easier to merge it there. That way will work transparent. What do you think? – VAAA Jul 12 '12 at 18:32
  • 1
    It also makes sense. Try the following http://knockoutmvc.com/ maybe you will find something usefull – Madman Jul 12 '12 at 18:42
  • @Madman I looked into that, and was very excited at first about writing c# thats translated to knockout. Then I saw in the click counter example that the way you handle methods is to send them all back to the server. That completely defeats the purpose of having those methods clientside! Its like you turned knockout into webforms! Your solution will have constant server chatter (sending the WHOLE MODEL every time), while a pure knockout solution can run happily on the client. WHY WOULD YOU DO THIS?! – Kyeotic Jul 12 '12 at 20:36
  • Yes, I agree with that about knockoutmvc thats why is better to create the model on server that match the one for knockout in the client. – VAAA Jul 12 '12 at 21:06
  • Yes guys, you are right, knockoutmvc is not good solution for the current problem. Maybe it will be applicable other time – Madman Jul 13 '12 at 09:32