Upon click of "Add" button, result from db will be fetched. I want this result to be populated in observableArray or computed (computed for future).
I am clueless on how to populate the observable array. Below is the code for ajax call
KNOCKOUTJS
self.addToCartViaProdList = function (item, event)
{
/*get current item index*/
/*in dom element $index() is enough but in viewModel a context is to be obtained*/
var context = ko.contextFor(event.target);
var index = context.$index();
var incQty = self.prodList()[index].ProductQty();
var prodID = self.prodList()[index].ProductID();
var verProdInCombo = ko.toJS({"prodID":prodID});
//check product in combos
$.ajax
({
url: '**************ProdID.php',
type: "post",
data: verProdInCombo,
success: function(result)
{
/*The result is in below format*/
/***************
"{"ComboName":"SinglePack","ComboItemsName":"Aloo Tikki Burger,Mango Lassi,Tomato Soup","ComboItemsID":"101_1_11,1105_11_110,901_9_90","ComboTotalPrice":"90.25"}"
********************/
},
error: function(xhr, status){
alert(xhr);
alert(status);
}
});
};
I am getting the success result, but all my attempts to put this data in observableArray has not yielded any fruitful result.
I have tried saving the result in localSession. And then using a computed variable to pick values from that localSession,
Then I have tried to use ko.mapping.fromJS(result), this didnot work either
I want to display the list as received from server in a div as follows:
HTML CODE
<div class="comboSeg" >
<div data-bind="foreach: comboDataList">
<div data-bind="text: ComboName">
</div>
<div data-bind="text: ComboItemsName">
</div>
<div data-bind="text: ComboTotalPrice">
</div>
</div>
</div>
The different version of knockoutjs tried are:
VERSION 1: Saving data in local session and using computed
self.comboDataList = ko.computed(function()
{
var comboDataItems = [];
if (self.comboValChange() == "DO" && localStorage.getItem("comboDataItem") != null)
{
var dataSaved = localStorage.getItem("comboDataItem");
comboDataItems = ko.utils.arrayMap(dataSaved, function(item)
{
return new ComboList(item.ComboName, item.ComboItemsName, item.ComboItemsID, item.ComboTotalPrice);
});
comboDataItems.push(dataSaved);
}
return comboDataItems;
},this);
//Ajax call is as follows:
$.ajax
({
url: '***************ProdID.php',
type: "post",
data: verProdInCombo,
success: function(result)
{
localStorage.removeItem("comboDataItem");
localStorage.setItem("comboDataItem",result);
//Forcing the computed function to be called, just a workaround
self.comboValChange("");
self.comboValChange("DO");
}, //Result will contain whatever server will send back as a mesage
error: function(xhr, status){
alert(xhr);
alert(status);
}
});
VERSION 2 : Pushing the success msg received in comboDataList
$.ajax
({
url: '*************ProdID.php',
type: "post",
data: verProdInCombo,
success: function(result)
{
self.comboDataList.push(result);
//Also tried below one
//self.comboDataList.push(ko.toJS(result));
}, //Result will contain whatever server will send back as a mesage
error: function(xhr, status){
alert(xhr);
alert(status);
}
});
Now I have enabled the denug in browser I got this error message for both the versions:
Error : Uncaught ReferenceError: Unable to process binding "text: function (){return ComboName }" Message: ComboName is not defined
successhandler function, but haven't included the code or the details as to why it didn't work or what error it gave. Please include those so we can help you fix the problem.successhandler is commented out ;-). If you add the code it's quite likely we'll be able to help you. - In any case, to answer your other question: Knockout will update the view automatically if you change observables, but not if you overwrite a property/variable that contains an observable with a new one. (Again though: easier to explain in the context of actual code.)