Knockout: partial display of arrays

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.

+3
source share
1 answer

This is a common question for the mapping plugin. There is currently no way to do this with the plugin itself. It has been a while. The plugin assumes that the array you gave it is the new contents of the array, so it removes other elements.

, , - .

http://jsfiddle.net/madcapnmckay/5878E/

.

, .

+4

All Articles