XF 2.3 How to unregister a javascript binding?

Jaxel

Well-known member
I have a function bound to window scrolling:
Code:
        init: function()
        {
            XF.on(window, 'scroll', this.scroll.bind(this));
        },
        
        scroll: function(e)
        {
            console.log('scrolling');
        },

How would I go on to unbind this function? I have tried the following, but it unbinds ALL scrolling functions, not just the one this.scroll function:
Code:
XF.off(window, 'scroll');
 
The events support namespaces as they did with jQuery. So just add e.g. .myscroll to the event names and only those specific events will be unbound.
 
Not quite sure how that is applied. I tried replacing 'scroll' with 'scroll.infinite' in both functions and it didn't work.
 
That is what should work, and you can see a few places where we do that in the core code, e.g.

JavaScript:
this.scrollWatchers.forEach(watcher => XF.off(watcher, 'scroll.autocomplete'))

// ...

XF.on(element, 'scroll.autocomplete', () =>
{
    setPositioning(this.results, positionData)
})

In both of these cases it will split the event name into an event and a namespace so it will bind a scroll event but in our case the autocomplete namespace. That way the off call will only affect the scroll events bound in the autocomplete namespace without affecting any others.
 
Back
Top Bottom