Unable to parse bindings js error using ko.mapping.fromJSON with parse exist view model

前端 未结 1 883
梦谈多话
梦谈多话 2020-12-16 07:18

I want to save view model in the hidden field in JSON format. All work fine.

But when I try to get it - I get error:

Uncaught Error: Unable to

相关标签:
1条回答
  • 2020-12-16 07:33

    You are calling the ko.mapping.fromJSON with wrong arguments.

    The correct usage in your case is the following:

    var viewModelDeserialized = 
        ko.mapping.fromJSON(serializedJsonString, {} /* empty options */, viewModel);
    

    Demo fiddle. (without the binding error)

    The usage of the ko.mapping.fromJSON method is a little bit tricky:

    • you can call it with one argument: providing just the data e.g var viewModel = ko.mapping.fromJSON(data) in this case it will return the created viewModel

    • you can call with two arguments:

      • if the second argument is a ko mapping created viewModel then it is treated as the mapping target ko.mapping.fromJSON(data, koMappingCreatedViewModel)
      • otherwise the second argument is treated as the mapping options (this happens in your case) var viewModel = ko.mapping.fromJSON(data, options)
    • you can call it with three arguments explicitly specifing the data, mapping and target: ko.mapping.fromJSON(data, options, target)

    0 讨论(0)
提交回复
热议问题