Last active
October 15, 2015 15:26
-
-
Save Deraen/9410308 to your computer and use it in GitHub Desktop.
A way to render Knockout ObservableArray responsively
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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