Problem saving part of viewModel and loading it again

49 views
Skip to first unread message

Andreas Kroll

unread,
Jul 28, 2016, 8:46:58 PM7/28/16
to KnockoutJS
Hi all, 

I have to save and load parts of the viewModel to disk. 
This all works fine until I have nested elements in the data. 

Please look at this jsfiddle: https://jsfiddle.net/b2dhbyvf/2/

I have setup the fiddle so data is already visible on the screen. 
As soon as you press Save and Load the data is mapped to JSON. 

This JSON would normally be saved to disk and later be loaded again. 

I simulate this by emptying viewModel.data.sections. 

Then I load the JSON again by mapping it in the viewModel.data element. 

This loads the name observable fine, but the type observable, which contains the selected element from the types observableArray gets eliminated by this process. 

WHY? 

I cannot seem to get this to work. 


Please help!

Best regards,
  Andreas Kroll

Andreas Kroll

unread,
Jul 28, 2016, 8:59:56 PM7/28/16
to KnockoutJS
I forgot to mention that I also tried using ko.toJSON while saving the json file. 

                //var json = ko.mapping.toJSON(viewModel.data);
                var json = ko.toJSON(viewModel.data);


This resultet in the data being in the JSON, but also not being loaded properly back into the viewModel.

noirabys

unread,
Jul 29, 2016, 3:29:41 AM7/29/16
to KnockoutJS

Andreas Kroll

unread,
Jul 29, 2016, 6:04:12 AM7/29/16
to KnockoutJS
Hi noirabys, 

thank you very much for your solution. 
Works fine so far, but I have a problem with that kind of solution: 

The code would depend on knowledge of the data model it normally should not have. 
So e.g. the first information is about the name of the property "type", the second is about the name of the lookup list to use (types). 

Using this solution would mean I would have to modify the mappingOptions every time the structure of the viewModel changes. 

The only item "connecting" the type property and the types observableArray is the select tag and its binding. 

Any idea how else this could be solved?
I also thought about having some "metadata" on the type property telling the importing code where to look for the list, but I never saw anything like that performed with knockout.js.

Thanks
  Andreas

noirabys

unread,
Jul 29, 2016, 9:08:20 AM7/29/16
to KnockoutJS
hi,
i don't know the mapping extension very well.
maybe you should have a look into the mapping sources or code a generic way on your own.
well, i prefer to have a static model at the client, which can be initialized with json data. 

Andreas Kroll

unread,
Jul 29, 2016, 7:26:15 PM7/29/16
to KnockoutJS
Hi again Noirabys, 

since I did not come up with another solution, I used the suggestion you created for me, and it all works perfectly. 
The only problem I see is, that it heavily depends on fieldnames, I had to rename a few because different locations used the same name, but had to use different lists as source. 
But I could create a working prototyp with the solution you found. 

Thank you very much for your help! 

Looking at the JSON file created during Save of the configuration data, I was wondering if I could use the same solutiontype to strip all additional data from the lookup fields. Currently each select* field renders the complete item of the source list in the JSON file, which is very prone to errors. 
Do you have any suggestions how to do that? 

Thanks for your help. 

Andreas

Gunnar Liljas

unread,
Jul 29, 2016, 7:26:49 PM7/29/16
to knock...@googlegroups.com
I rarely use the mapping plugin. Mapping "manually" is a bit tedious, but in the end it saves time to be in control.

/G
Reply all
Reply to author
Forward
0 new messages