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 .
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.
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());
}
}