Skip to content

Commit 04d21d9

Browse files
committed
moved Adapter definition out of the linker function
1 parent f29eec6 commit 04d21d9

File tree

1 file changed

+60
-66
lines changed

1 file changed

+60
-66
lines changed

src/ui-scroll.coffee

Lines changed: 60 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,52 @@ angular.module('ui.scroll', [])
229229
buffer.remove(0, overage)
230230
buffer.first += overage
231231

232+
viewport
232233

234+
Adapter = (buffer, adjustBuffer) ->
235+
this.isLoading = false
233236

234-
viewport
237+
applyUpdate = (wrapper, newItems) ->
238+
if angular.isArray newItems
239+
pos = (buffer.indexOf wrapper) + 1
240+
for newItem,i in newItems.reverse()
241+
if newItem == wrapper.item
242+
keepIt = true;
243+
pos--
244+
else
245+
buffer.insert pos, newItem
246+
unless keepIt
247+
wrapper.op = 'remove'
248+
249+
this.applyUpdates = (arg1, arg2) ->
250+
if angular.isFunction arg1
251+
# arg1 is the updater function, arg2 is ignored
252+
bufferClone = buffer.slice(0)
253+
for wrapper,i in bufferClone # we need to do it on the buffer clone, because buffer content
254+
# may change as we iterate through
255+
applyUpdate wrapper, arg1(wrapper.item, wrapper.scope, wrapper.element)
256+
else
257+
# arg1 is item index, arg2 is the newItems array
258+
if arg1%1 == 0 # checking if it is an integer
259+
if 0 <= arg1-buffer.first < buffer.length
260+
applyUpdate buffer[arg1 - buffer.first], arg2
261+
else
262+
throw new Error 'applyUpdates - ' + arg1 + ' is not a valid index'
263+
adjustBuffer()
264+
265+
this.append = (newItems) ->
266+
for item in newItems
267+
++buffer.next
268+
buffer.insert 'append', item
269+
adjustBuffer()
235270

271+
this.prepend = (newItems) ->
272+
for item in newItems.reverse()
273+
--buffer.first
274+
buffer.insert 'prepend', item
275+
adjustBuffer()
276+
277+
return
236278

237279
require: ['?^uiScrollViewport']
238280
transclude: 'element'
@@ -261,7 +303,6 @@ angular.module('ui.scroll', [])
261303
throw new Error datasourceName + ' is not a valid datasource'
262304

263305
bufferSize = Math.max(3, +$attr.bufferSize || 10)
264-
bufferPadding = -> viewport.outerHeight() * Math.max(0.1, +$attr.padding || 0.1) # some extra space to initiate preload
265306

266307
# initial settings
267308

@@ -271,6 +312,19 @@ angular.module('ui.scroll', [])
271312

272313
viewport = new Viewport(buffer, element, controllers, $attr.padding)
273314

315+
adapter = new Adapter buffer,
316+
->
317+
dismissPendingRequests()
318+
adjustBuffer ridActual
319+
320+
if $attr.adapter # so we have an adapter on $scope
321+
adapterOnScope = $parse($attr.adapter)($scope)
322+
if not angular.isObject adapterOnScope
323+
$parse($attr.adapter).assign($scope, {})
324+
adapterOnScope = $parse($attr.adapter)($scope)
325+
angular.extend(adapterOnScope, adapter)
326+
adapter = adapterOnScope
327+
274328
# Padding element builder
275329
#
276330
# Calling linker is the only way I found to get access to the tag name of the template
@@ -305,13 +359,15 @@ angular.module('ui.scroll', [])
305359
ridActual++
306360
pending = []
307361

308-
reloadImpl = ->
362+
reload = ->
309363
dismissPendingRequests()
310364
buffer.clear()
311365
viewport.topPadding(0)
312366
viewport.bottomPadding(0)
313367
adjustBuffer ridActual
314368

369+
adapter.reload = reload
370+
315371
enqueueFetch = (rid, direction)->
316372
if !adapter.isLoading
317373
loading(true)
@@ -501,7 +557,7 @@ angular.module('ui.scroll', [])
501557
viewport.bind 'scroll', resizeAndScrollHandler
502558
viewport.bind 'mousewheel', wheelHandler
503559

504-
$scope.$watch datasource.revision, reloadImpl
560+
$scope.$watch datasource.revision, reload
505561

506562
$scope.$on '$destroy', ->
507563
# clear the buffer. It is necessary to remove the elements and $destroy the scopes
@@ -510,68 +566,6 @@ angular.module('ui.scroll', [])
510566
viewport.unbind 'scroll', resizeAndScrollHandler
511567
viewport.unbind 'mousewheel', wheelHandler
512568

513-
514-
# adapter setup
515-
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
544-
else
545-
throw new Error 'applyUpdates - ' + arg1 + ' is not a valid index'
546-
adjustBuffer()
547-
548-
this.append = (newItems) ->
549-
for item in newItems
550-
++buffer.next
551-
buffer.insert 'append', item
552-
adjustBuffer()
553-
554-
this.prepend = (newItems) ->
555-
for item in newItems.reverse()
556-
--buffer.first
557-
buffer.insert 'prepend', item
558-
adjustBuffer()
559-
560-
return
561-
562-
adapter = new Adapter buffer,
563-
->
564-
dismissPendingRequests()
565-
adjustBuffer ridActual
566-
567-
if $attr.adapter # so we have an adapter on $scope
568-
adapterOnScope = $parse($attr.adapter)($scope)
569-
if not angular.isObject adapterOnScope
570-
$parse($attr.adapter).assign($scope, {})
571-
adapterOnScope = $parse($attr.adapter)($scope)
572-
angular.extend(adapterOnScope, adapter)
573-
adapter = adapterOnScope
574-
575569
# update events (deprecated since v1.1.0, unsupported since 1.2.0)
576570

577571
unsupportedMethod = (token) ->

0 commit comments

Comments
 (0)