Check out this short example .
<select data-bind="options: Days, optionsText: 'title'"></select>
<input type="button" value="update" data-bind="click: update" />
<script type="text/javascript">
var Days = {'Days': [{"id":1,"title":"Monday"},{"id":2,"title":"Tuesday"},{"id":3,"title":"Wensday"}]};
var DaysUpdate = {'Days': [{"id":3,"title":"Wednesday"},{"id":4,"title":"Thursday"},{"id":5,"title":"Friday"}]};
var mapping = {
'Days': {
key: function(data) {
return ko.utils.unwrapObservable(data.id);
}
}}
var viewModel = {
Days: ko.observableArray(),
update: function() {
ko.mapping.fromJS(DaysUpdate, mapping, viewModel);
}
}
ko.mapping.fromJS(Days, mapping, viewModel);
ko.applyBindings(viewModel);
</script>
In this example, the data is partially displayed. First from the Days object, then (by clicking the refresh button) from the DateUpdate object. The second update removes the Monday and Tuesday objects from the array. How do i do this?
PS. Thanks to Mark Robinson for the best structured example.
source
share