Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updateFromJS broken? #6

Closed
abp opened this issue Jan 19, 2011 · 5 comments
Closed

updateFromJS broken? #6

abp opened this issue Jan 19, 2011 · 5 comments

Comments

@abp
Copy link

abp commented Jan 19, 2011

Hi,

i'm trying to use updateFromJS to merge data back into a fairly complex viewModel after the data is saved and probably changed on the server.

But it seems updateFromJS is replacing all the data instead of merging. I thought it would merge. So, am i doing something wrong or is it broken?

Here's a sample test case:
http://jsfiddle.net/rBRJZ/2/

@sagacity
Copy link
Collaborator

Hi abp,

This is by design. Basically, the model you pass to updateFromJS represents the 'current state' of your model that comes from your server. The mapping plugin then takes this current state and updates your mapped viewModel to match this state, so that your GUI also shows the current state.

In your example, therefore, it would remove the elements with keys '1' and '3' since they are not in your 'current state'. In a real-life scenario, maybe this is because some other user deleted items #1 and #3 since the last update occured.

If you want to do regular merging, that would be something you'd have to do yourself, manually.

I hope this explains why you are seeing this behavior. If not, feel free to reopen this ticket :)

@abp
Copy link
Author

abp commented Jan 19, 2011

Sure, answers my question right to the point. Sorry for reporting a bug on this. :)
Since my model is complex and rendering everything again would take far to much time i will code some simple merging by myself.

@sagacity
Copy link
Collaborator

No, that's okay! When writing your merge code, please note there's a useful "ko.utils.compareArrays" available that allows you to do diffs of two arrays. That can be a good starting point for you, I think.

@abp
Copy link
Author

abp commented Jan 19, 2011

Thanks for the hint, but now i already have a dead simple remove/add based merging. Will see how it works out. :)

@sagacity
Copy link
Collaborator

Excellent! Good luck :)

pkmccaffrey pushed a commit to pkmccaffrey/knockout.mapping that referenced this issue Aug 5, 2015
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants