Unable to map the Data using mapping plugin ? Knockout

116 views Asked by At

I am trying to bind my data to view but i am unsuccessful in all my attempts .

There is a Array of data i'm storing in a variable and later using mapping plugin and i am making those as observable's to bind it to view .

Interesting thing here is i am getting no errors in console and i checked with

<span data-bind="text: ko.toJSON(Item)"></span> 

I can see my array but i can't see it binded to view .

Script code :

console.log(ko.mapping.fromJS(jsonData));
var viewModel = new MainModel();
viewModel.Item(ko.mapping.fromJS(jsonData));
ko.applyBindings(viewModel);

Fiddle link is Here .

This is my first trail on the mapping plugin .Please do suggest me a reference to build some complex things using mapping plugin .

Any better approach is advisable if .

2

There are 2 answers

2
dotnetstep On BEST ANSWER

I don't remember but it is same as question I ask two days back.

I think if you want similar once problem I have solved it and you can find it on your updated fiddle. I also updated knockout version to 3.2.0.

http://jsfiddle.net/C46pU/9/

var mapping = {
    'Items': {
        create: function(options) {
            console.log('Inside Mapping Item');
            return new ChildModel(options.data);
        }
    },
     'Value': {
         create: function(options){
                  console.log('Inside Mapping Value');
                  return new ChildModel(options.data);
         }
       }
 };

$(document).ready(function () {

           var jsonData = {"Items":[{"Value":[{"Value1":"1","Value2":"2"},{"Value1":"3","Value2":"4"}]},{"SelectedOption":["Item2"],"Value":[{"Value1":"5","Value2":"6"},{"Value1":"7","Value2":"8"},{"Value1":"9","Value2":"10"}]}]};

         var viewModel = new MainModel(jsonData);                    
         ko.applyBindings(viewModel);                         
        });

        function ValueModel() {
            var self = this;
            self.Value1 = ko.observable();
            self.Value2 = ko.observable();
        }

        function ChildModel(data) {
            var self = this;
            self.SelectedOption = ko.observable();
            self.Value = ko.observableArray([]);
            if(data != null)
            {
                console.log(data);
                ko.mapping.fromJS(data,mapping,self);
            }
            self.AddValue = function () {
                self.Value.push(new ValueModel());
            }
        }


        function MainModel(data) {
            var self = this;            
            self.Items = ko.observableArray([]);
            if(data != null)
            {
                console.log('Inside Main Model');
                console.log(data);
                ko.mapping.fromJS(data,mapping, self);
            }           

            self.dropDownItem = ko.observableArray(['Item1', 'Item2', 'Item3', 'Item4']);

            self.AddItem = function () {
                self.Items.push(new ChildModel());
            }
        }
1
TSV On

I've modified your fiddle, adding the "mapping" defenition and some other changes (i'm afraid of lost the sources, thus i posted them here).

The main thing is

var mapping = {
    'Item': {
        create: function(options) {
            return new ChildModel(options.data);
        }
    }
} 

where mapping plugin will be forced to use certain object creation function then deserializes your cild model.

Markup:

    <h2>Nested</h2>
<table  cellpadding="10" cellspacing="5" style="padding:10px;">
    <tbody data-bind="foreach: Item">
    <tr >
        <td>
            <select data-bind="options: $root.dropDownItem, selectedOptions: $data.SelectedOption && SelectedOption() || ''"></select>
        </td>
        <td>
            <input type="button" data-bind="click: AddValue" value="Add Value" />
        </td>
        <td>
            <table data-bind="foreach: Value">
                <tr>
                    <td><input type="text" data-bind="value: Value1" /></td>
                    <td><input type="text" data-bind="value: Value2"/></td>
                </tr>
            </table>
        </td>
    </tr>
    </tbody>
</table>
<div>
    <input type="button" value="Add Item" data-bind="click: AddItem" />
</div>
<div>
    <span data-bind="text: ko.toJSON(Item)"></span>
</div>

Code:

 $(document).ready(function () {

           var jsonData = {"Item":[{"Value":[{"Value1":"1","Value2":"2"},{"Value1":"3","Value2":"4"}]},{"SelectedOption":["Item2"],"Value":[{"Value1":"5","Value2":"6"},{"Value1":"7","Value2":"8"},{"Value1":"9","Value2":"10"}]}]};
var mapping = {
    'Item': {
        create: function(options) {
            return new ChildModel(options.data);
        }
    }
}
     var viewModel = new MainModel();
          viewModel.Item(ko.mapping.fromJS(jsonData, mapping).Item());
ko.applyBindings(viewModel);    

        });

        function ValueModel() {
            var self = this;
            self.Value1 = ko.observable();
            self.Value2 = ko.observable();
        }

        function ChildModel() {
            var self = this;
            self.SelectedOption = ko.observable();
            self.Value = ko.observableArray([new ValueModel()]);

            self.AddValue = function () {
                self.Value.push(new ValueModel());
            }
        }


        function MainModel() {
            var self = this;            
            self.Item = ko.observableArray([]);

            init();

            function init() {
                self.Item.push(new ChildModel());                
            }

            self.dropDownItem = ko.observableArray(['Item1', 'Item2', 'Item3', 'Item4']);

            self.AddItem = function () {
                self.Item.push(new ChildModel());
            }
        }

Hope, it helps.