Skip to content

Commit f29eec6

Browse files
committed
Adapter object
1 parent 5d7bb39 commit f29eec6

File tree

1 file changed

+48
-44
lines changed

1 file changed

+48
-44
lines changed

src/ui-scroll.coffee

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ angular.module('ui.scroll', [])
8282
itemScope[itemName] = item
8383
wrapper =
8484
scope: itemScope
85+
item: item
8586

8687
linker itemScope, (clone) ->
8788
wrapper.element = clone
@@ -287,7 +288,7 @@ angular.module('ui.scroll', [])
287288
viewportScope = viewport.scope() || $rootScope
288289

289290
topVisible = (item) ->
290-
adapter.topVisible = item.scope[itemName]
291+
adapter.topVisible = item.item
291292
adapter.topVisibleElement = item.element
292293
adapter.topVisibleScope = item.scope
293294
$parse($attr.topVisible).assign(viewportScope, adapter.topVisible) if $attr.topVisible
@@ -512,52 +513,56 @@ angular.module('ui.scroll', [])
512513

513514
# adapter setup
514515

515-
adapter = {}
516-
adapter.isLoading = false
517-
adapter.reload = reloadImpl
518-
519-
applyUpdate = (wrapper, newItems) ->
520-
if angular.isArray newItems
521-
pos = (buffer.indexOf wrapper) + 1
522-
for newItem,i in newItems.reverse()
523-
if newItem == wrapper.scope[itemName]
524-
keepIt = true;
525-
pos--
516+
Adapter = (buffer, adjustBuffer) ->
517+
this.isLoading = false
518+
this.reload = reloadImpl
519+
520+
applyUpdate = (wrapper, newItems) ->
521+
if angular.isArray newItems
522+
pos = (buffer.indexOf wrapper) + 1
523+
for newItem,i in newItems.reverse()
524+
if newItem == wrapper.item
525+
keepIt = true;
526+
pos--
527+
else
528+
buffer.insert pos, newItem
529+
unless keepIt
530+
wrapper.op = 'remove'
531+
532+
this.applyUpdates = (arg1, arg2) ->
533+
if angular.isFunction arg1
534+
# arg1 is the updater function, arg2 is ignored
535+
bufferClone = buffer.slice(0)
536+
for wrapper,i in bufferClone # we need to do it on the buffer clone, because buffer content
537+
# may change as we iterate through
538+
applyUpdate wrapper, arg1(wrapper.item, wrapper.scope, wrapper.element)
539+
else
540+
# arg1 is item index, arg2 is the newItems array
541+
if arg1%1 == 0 # checking if it is an integer
542+
if 0 <= arg1-buffer.first < buffer.length
543+
applyUpdate buffer[arg1 - buffer.first], arg2
526544
else
527-
buffer.insert pos, newItem
528-
unless keepIt
529-
wrapper.op = 'remove'
545+
throw new Error 'applyUpdates - ' + arg1 + ' is not a valid index'
546+
adjustBuffer()
530547

531-
adapter.applyUpdates = (arg1, arg2) ->
532-
dismissPendingRequests()
533-
if angular.isFunction arg1
534-
# arg1 is the updater function, arg2 is ignored
535-
bufferClone = buffer.slice(0)
536-
for wrapper,i in bufferClone # we need to do it on the buffer clone, because buffer content
537-
# may change as we iterate through
538-
applyUpdate wrapper, arg1(wrapper.scope[itemName], wrapper.scope, wrapper.element)
539-
else
540-
# arg1 is item index, arg2 is the newItems array
541-
if arg1%1 == 0 # checking if it is an integer
542-
if 0 <= arg1-buffer.first < buffer.length
543-
applyUpdate buffer[arg1 - buffer.first], arg2
544-
else
545-
throw new Error 'applyUpdates - ' + arg1 + ' is not a valid index'
546-
adjustBuffer ridActual
548+
this.append = (newItems) ->
549+
for item in newItems
550+
++buffer.next
551+
buffer.insert 'append', item
552+
adjustBuffer()
547553

548-
adapter.append = (newItems) ->
549-
dismissPendingRequests()
550-
for item in newItems
551-
++buffer.next
552-
buffer.insert 'append', item
553-
adjustBuffer ridActual
554+
this.prepend = (newItems) ->
555+
for item in newItems.reverse()
556+
--buffer.first
557+
buffer.insert 'prepend', item
558+
adjustBuffer()
554559

555-
adapter.prepend = (newItems) ->
556-
dismissPendingRequests()
557-
for item in newItems.reverse()
558-
--buffer.first
559-
buffer.insert 'prepend', item
560-
adjustBuffer ridActual
560+
return
561+
562+
adapter = new Adapter buffer,
563+
->
564+
dismissPendingRequests()
565+
adjustBuffer ridActual
561566

562567
if $attr.adapter # so we have an adapter on $scope
563568
adapterOnScope = $parse($attr.adapter)($scope)
@@ -567,7 +572,6 @@ angular.module('ui.scroll', [])
567572
angular.extend(adapterOnScope, adapter)
568573
adapter = adapterOnScope
569574

570-
571575
# update events (deprecated since v1.1.0, unsupported since 1.2.0)
572576

573577
unsupportedMethod = (token) ->

0 commit comments

Comments
 (0)