Search code examples
jquery-waypoints

Destroy all waypoints on an element from within one of the waypoints handlers


I have a couple of waypoints attached to each 'new comment' in a list of comments that will mark the comment as read when it comes into view (either up or down). I based my code off the Inview shortcut file but that didn't cut the mustard as it didn't support the $.waypoint method of bulk attaching waypoints.

My question is, how can I destroy both the up and down waypoints on a particular element from within one of the waypoint handlers? I can't see a way of retrieving all the waypoints for a particular element in the API.

content.find('.comment').has('.metadata > span.new').waypoint
  offset: 'bottom-in-view'
  context: '.content'
  handler: (direction) ->
    comment = $(this.element)
    if direction == 'down'
      this.destroy()
      console.log 'Mark as read'

content.find('.comment').has('.metadata > span.new').waypoint
  offset: 0
  context: '.content'
  handler: (direction) ->
    comment = $(this.element)
    if direction == 'up'
      this.destroy()
      console.log 'Mark as read'

Forgive me the coffeescript ;) I also plan to DRY this all up in the end.


Solution

  • Turns out it was better to use the Inview shortcut as you can use its destroy() method within its handlers. The Inview destroy method destroys all of the 4 waypoint types added when one creates an Inview.

    for comment in content.find('.comment').has('.metadata > span.new')
      do (comment) ->
        new Waypoint.Inview
          element: comment
          context: content[0]
          entered: (direction) ->
            this.destroy()
            console.log 'Mark as read'