Skip to content

Commit

Permalink
fix(combo knockout): update ds when new ds length is the same #1852
Browse files Browse the repository at this point in the history
  • Loading branch information
Lipata committed Dec 6, 2018
1 parent 7fe463e commit d30a3af
Showing 1 changed file with 40 additions and 37 deletions.
77 changes: 40 additions & 37 deletions src/js/extensions/infragistics.ui.combo.knockout-extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,41 @@
}
}

function applyListItemsBindings(valueAccessor) {
var combo = valueAccessor().combo,
$comboList = combo.igCombo("listItems"),
options = valueAccessor().options,
dataSource = ko.utils.unwrapObservable(valueAccessor().dataSource),
i;

for (i = 0; i < $comboList.length; i++) {
if (ko.isObservable($comboList[ i ])) {
ko.applyBindingsToNode($comboList[ i ], {
igComboItem: {
combo: combo,
value: dataSource[ i ],
index: i,
options: options
}
}, dataSource[ i ]);
}
}
}

function identicalDataSources(combo, viewModelDataSource) {
var valueKey = combo.igCombo("option", "valueKey"), comboDataSource, index;

if (valueKey !== undefined) {
comboDataSource = ko.utils.unwrapObservable(combo.igCombo("option", "dataSource").data());
for (index = 0; index < comboDataSource.length; index++) {
if (comboDataSource[index][valueKey] !== viewModelDataSource[index][valueKey]) {
return false;
}
}
}
return true;
}

ko.bindingHandlers.igCombo = {
init: function (element, valueAccessor) {
var combo = $(element),
Expand Down Expand Up @@ -148,26 +183,7 @@

ko.bindingHandlers.igComboList = {
init: function (element, valueAccessor) {
var combo = valueAccessor().combo,
$comboList = combo.igCombo("listItems"),
options = valueAccessor().options,
dataSource = ko.utils.unwrapObservable(valueAccessor().dataSource),
i;

if (dataSource) {
for (i = 0; i < $comboList.length; i++) {
if (ko.isObservable($comboList[ i ])) {
ko.applyBindingsToNode($comboList[ i ], {
igComboItem: {
combo: combo,
value: dataSource[ i ],
index: i,
options: options
}
}, dataSource[ i ]);
}
}
}
applyListItemsBindings(valueAccessor);
},
update: function (element, valueAccessor) {
var combo = $(valueAccessor().combo),
Expand All @@ -176,30 +192,17 @@
dataSource = ko.utils.unwrapObservable(valueAccessor().dataSource),
dropDownScroller = combo.data("igCombo")._options.$dropDownScrollCont,
lastScrollTop = dropDownScroller ? dropDownScroller.scrollTop() : 0,
$comboList, i;
$comboList;

if (listLength !== dataSource.length) {
if (listLength !== dataSource.length || !identicalDataSources(combo, dataSource)) {
combo.one("igcomboitemsrendered", function () {
$comboList = combo.igCombo("listItems");
if (dataSource) {
for (i = 0; i < $comboList.length; i++) {
if (ko.isObservable($comboList[ i ])) {
ko.applyBindingsToNode($comboList[ i ], {
igComboItem: {
combo: combo,
value: dataSource[ i ],
index: i,
options: options
}
}, dataSource[ i ]);
}
}
}
applyListItemsBindings(valueAccessor);
selectItems(combo, valueAccessor().selectedItems);
});

// N.A. 8/5/2015 Bug #203826 Set datasource, cause in this case it is analyzed and then the dataBind happens.
// This necessay in cases, when data source was empty array initially.
// This necessary in cases, when data source was empty array initially.
combo.igCombo("option", "dataSource", dataSource);

// R.K. 29th November, 2017 #246482: When an item is selected from the bottom of the list,
Expand Down

0 comments on commit d30a3af

Please sign in to comment.