What happened to the triggerOnce option in Waypoints 3.0?

Deadly 提交于 2019-12-23 11:49:14

问题


I recently upgraded jquery-waypoints from 2.x to 3.x in my project and found that a lot of my code broke. All references to $(this) within my handlers had to be changed to $(this.element) which, once I figured out was the cause of my troubles, was easy enough to fix.

I haven't been able to figure out what happened to the triggerOnce option which would prevent a waypoint from firing multiple times. Any idea why this was removed and how can I achieve the same functionality?


回答1:


I know an answer was already marked as correct, but I'd like to expand beyond a simple comment.

3.0, being a major version, made breaking changes. One of them was the removal of triggerOnce. It is noted in the changelog. It is also mentioned in the destroy docs where using destroy at the end of the handler is called out as an alternative to the old triggerOnce.

Previously, triggerOnce wasn't the exact same as calling destroy because all Waypoint methods were called on jQuery objects. The elements in those jQuery objects may have multiple waypoints attached to them, but there was no way to separate them once they were created. If you called destroy all waypoints on that element were destroyed. The triggerOnce option, however, worked on an individual waypoint basis behind the scenes. Now that 3.0 returns instances of the Waypoint class directly and this within the handler is a reference to the Waypoint instance instead of the element, there is no difference between triggerOnce and calling this.destroy() to end a handler. So the code was removed.




回答2:


I figured out a solution. Adding this.disable() to the end of my handler disables the waypoint after it's been fired, preventing it from being called again. I really think this should be documented.




回答3:


If you need to disable a jQuery waypoint (v3.0):

$('.my-waypoint').waypoint(function(direction){
  // Do some stuff
  this.destroy();
});


来源:https://stackoverflow.com/questions/27629472/what-happened-to-the-triggeronce-option-in-waypoints-3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!