json - KnockoutJS - Databind to a dictionary collection - with update -
this question here knockoutjs - databind dictionary collection
i'm creating drop down select json coming server. @ point after creating wish update data. i've created fiddle http://jsfiddle.net/lprf3/
which shows i'm @ at present. update select's observable array. however... reason need click select drop down in order refresh
javascript:
$(function() { var destinationsfromserver = {"europe":"europe incl egypt, turkey & tunisia","anzo":"australia & new zealand","worldwideusa":"worldwide (incl usa & canada)"}; var updateddestinationsfromserver = {"arctic":"includes polar bears , seals","antarctic":"just penguins"}; function mapdictionarytoarray(dictionary) { var result = []; (var key in dictionary) { if (dictionary.hasownproperty(key)) { result.push({ key: key, value: dictionary[key] }); } } return result; } function viewmodel () { destinations= ko.observablearray(mapdictionarytoarray(destinationsfromserver)); selecteddestination= ko.observable(); updatedestinations = function() { destinations= ko.observablearray(mapdictionarytoarray(updateddestinationsfromserver)); }; }; ko.applybindings(new viewmodel()); });
html
<select data-bind="options: destinations, optionstext: 'key', optionsvalue: 'value', value: selecteddestination"></select> <hr /> <div data-bind="text: selecteddestination"></div> <button data-bind="click:updatedestinations">update</button>
how can select update?
you reassinging destinations
new observabelarray
instead of updating array. see this fiddle. when updating any observable, pass new value in parameter, never assign new value =
operatior.
wrong way:
updatedestinations = function(){ destinations=ko.observablearray(mapdictionarytoarray(updateddestinationsfromserver)); };
right way:
updatedestinations = function(){ destinations(mapdictionarytoarray(updateddestinationsfromserver)); };
Comments
Post a Comment