recursionHelper.coffee 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #
  2. # * An Angular service which helps with creating recursive directives.
  3. # * @author Mark Lagendijk
  4. # * @license MIT
  5. #
  6. # From: https://github.com/marklagendijk/angular-recursion
  7. angular.module("RecursionHelper", []).factory "RecursionHelper", [
  8. "$compile"
  9. ($compile) ->
  10. ###
  11. Manually compiles the element, fixing the recursion loop.
  12. @param element
  13. @param [link] A post-link function, or an object with function(s) registered via pre and post properties.
  14. @returns An object containing the linking functions.
  15. ###
  16. return compile: (element, link) ->
  17. # Normalize the link parameter
  18. link = post: link if angular.isFunction(link)
  19. # Break the recursion loop by removing the contents
  20. contents = element.contents().remove()
  21. compiledContents = undefined
  22. pre: (if (link and link.pre) then link.pre else null)
  23. ###
  24. Compiles and re-adds the contents
  25. ###
  26. post: (scope, element) ->
  27. # Compile the contents
  28. compiledContents = $compile(contents) unless compiledContents
  29. # Re-add the compiled contents to the element
  30. compiledContents scope, (clone) ->
  31. element.append clone
  32. return
  33. # Call the post-linking function, if any
  34. link.post.apply null, arguments if link and link.post
  35. return
  36. ]