Skip to content

Instantly share code, notes, and snippets.

@evanworley
Last active December 21, 2015 03:59
Show Gist options
  • Save evanworley/6246378 to your computer and use it in GitHub Desktop.
Save evanworley/6246378 to your computer and use it in GitHub Desktop.
ko.extenders.progressiveArray = (target, opts = {}) ->
target.progressive = _.extend({
batchSize: 10
}, opts)
# Utility function for adding without value mutation events
target.add ?= (item) -> target().push(item)
target.clear ?= () -> target([])
target.updateProgressive = (items) ->
status = $.Deferred()
target.clear()
addItems = (items) ->
remaining = items.slice(target.progressive.batchSize)
items = items.slice(0, target.progressive.batchSize)
for item in items
target.add(item)
target.valueHasMutated()
if remaining.length > 0
requestAnimationFrame(() -> addItems(remaining))
else
status.resolve()
addItems(items)
return status
return target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment