Skip to content

Instantly share code, notes, and snippets.

@nmccready
Forked from rkgarg/post-repeat-directive.js
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nmccready/9d564782608d7c83aacf to your computer and use it in GitHub Desktop.
Save nmccready/9d564782608d7c83aacf to your computer and use it in GitHub Desktop.
#shamelessly copied from
#http://tech.small-improvements.com/2013/09/10/angularjs-performance-with-large-lists/
#https://gist.github.com/rkgarg/7232175
app = require '../../app.coffee'
directiveName = 'postRepeat'
app.directive directiveName, [ '$log',
($log) ->
postRepeat = {}
scope:
options: '='
link: (scope, element, attrs) ->
attrScope = scope.$parent
#root parent to all ng-repeats (encapsulating div or whatever)
parent = attrScope.$parent
if attrScope.$first
# lastTime can be updated anywhere if to reset counter at some
#action if ng-repeat is not getting started from $first
postRepeat[parent.$id] =
lastTime: new Date()
parent.$on '$destroy', ->
delete postRepeat[parent.$id]
if scope.options?
opts = scope.options
#use init function (via attribute binding (basically options)) to
# pass the postRepeat object on so the lastTime can be rest
opts.init(postRepeat[parent.$id],scope) if opts.init? and angular.isFunction opts.init
if attrScope.$last
scope.$evalAsync ->
$log.debug "## DOM rendering list took: " +
(new Date() - postRepeat[parent.$id].lastTime) + " ms"
doDelete = if not opts?.doDeleteLastTime? then true else opts?.doDeleteLastTime
delete postRepeat[parent.$id] if doDelete
]
app.controller 'Ctrl', ($scope) ->
_postRepeat = null
$scope =
postRepeatOptions:
init: (postRepeat, scope) ->
_postRepeat = postRepeat
doDeleteLastTime: false
//some where down the line ...
//this is useful for testing infinite scroll where you need to not always erase lastTime untill specific momments
postRepeat.lastTime = new Date()
<tr ng-repeat="item in items" post-repeat options="postRepeatOptions">…</tr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment