Knockout JS mapping plugin confusion

前端 未结 1 685
粉色の甜心
粉色の甜心 2021-01-22 02:46

I\'m confused as to when and where I should declare my viewModel when using the mapping plugin.

Here\'s my json file:

{
    \"members\": [
        {
            


        
相关标签:
1条回答
  • 2021-01-22 03:12

    You likely want to declare your viewModel outside of the closure, so it is more accessible. For example:

    var viewModel = {};
    var data = $.getJSON("members.json",function(data)  
                {
                    viewModel.model = ko.mapping.fromJSON(data);
                     ko.applyBindings(viewModel);
                }
            );
    

    This would create the viewModel, make it accessible, and expose the model property (which would contain all the mapped data). You could skip the model property and just do it on the vm, too. You could even move the applyBindings outside of this, since you really only want that to run once.

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