Skip to content

Instantly share code, notes, and snippets.

@Deraen
Last active October 15, 2015 15:26
Show Gist options
  • Select an option

  • Save Deraen/9410308 to your computer and use it in GitHub Desktop.

Select an option

Save Deraen/9410308 to your computer and use it in GitHub Desktop.
A way to render Knockout ObservableArray responsively
var newArray = [{foo: 'bar', ...}, ...];
// This will block the UI thread during the rendering. It will become problem if the array has around 1000 items.
self.obsArray(newArray);
// This way UI should be responsive during the rendering
throttledPush(self.obsArray, newArray);
function throttledPush(obsArray, array, items, timeout) {
items = items || 5;
timeout = timeout || 1;
var cancel = false;
var i = 0;
function addItems() {
if (cancel) return;
obsArray.valueWillMutate();
for (var j = 0; j < items && i < array.length; ++j && ++i) {
obsArray().push(array[i]);
}
obsArray.valueHasMutated();
if (i < array.length) setTimeout(addItems, timeout);
}
addItems();
return {
cancel: function() {
cancel = true;
},
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment