Hi all,
I'm having a bit of a problem having an observable array of objects to work.
I have to load the array from a separate JS array and create a new item for each element of the source array:
self.Votes = ko.observableArray([]);
self.Votes = $.map(Countries, function(item) { return new StateVote(item) });
function StateVote(country){
this.Country=country;
this.Vote=ko.observable();
this.Partecipating=ko.observable('Yes');
}
Then the Votes array is bound to a list, and the Vote and Participating properties are bound to select elements.
<tbody data-bind="foreach: Votes">
<tr>
<td data-bind="text: Country.Name"></td>
<td><select data-bind="options: ['Choose...','Yes','No','Abstain'], value: Vote"></select></td>
<td><select data-bind="options: ['Yes','No'], value: Partecipating"></select></td>
</tr>
</tbody>
My problem is: if I load the array like I've just shown, I get binding on the properties Vote and Partecipating (if I change them in the select I see computed values change, and if I change them programmaticaly, the seleceted element changes. But I cannot sort the list.
While if I load the array like:
self.Votes = ko.observableArray($.map(Countries, function(item) { return new StateVote(item) }));
I get the sorting on list, but the single properties are not bound any more (changing their value doesn't make computed to change, and UI is not updated when changing values programmticaly)
Any idea of what I'm doing wrong?
Thank you
Simone
PS:
if relevant the Countries array is defined as:
Countries = [
{
"Code":"BE",
"Name":"Belgium",
"OriginalName":"Belgique / Belgie",
"Votes":12,
"Population":11007020
},
{
"Code":"BG",
"Name":"Bulgaria",
"OriginalName":"Bulgaria",
"Votes":10,
"Population":7364570
},
...........