We have an observableArray in Knockout which include several JSON objects. Under each JSON object we have an nested array which needs to be observable.
Knockout is not able to observe arrays nested in each JSON object, in the observableArray.
Is it possible to map an array which is already nested in an observableArray?
Here is an example of one JSON object in the observableArray.
Note: We need to make the "attributeValues" array observable.
{
"attribute": {
"id": 6,
"name": "Some attribute name",
"typeID": 5
},
"type": {
"id": 5,
"typeName": "list"
},
"attributeValues": [{
"id": 10,
"attributeID": 6,
"value": "Some attribute value",
"chosen": false
}, {
"id": 11,
"attributeID": 6,
"value": "Another attribute value",
"chosen": false
}, {
"id": 12,
"attributeID": 6,
"value": "Third attribute value",
"chosen": false
}]
}
Here is the code we're using now:
$.ajax({
type: 'GET',
url: '/JsonService',
success: function (data) {
avm.attributes(data.allAttributes);
},
dataType: 'json',
traditional: true
});
function attributeViewModel() {
var self = this;
self.attributes = ko.observableArray([]);
}
var avm = new attributeViewModel();
ko.applyBindings(avm);