Picture of fergus beake
creating Scroll Events
by fergus beake - Tuesday, 17 April 2018, 3:59 PM
 

Hi,

I'm currently attempting to make a scroll event in a plugin,

what would be the best way to implement a scroll event into Adapts backbone model?

I'm currently using 

 

initialize: function() {
_.bindAll(this, 'detect_scroll');
// bind to window
$(window).scroll(this.onScroll);
},
 
immediately after the componentView is extended, but this doesn't appear to work.
 
Thanks,
 
Picture of Matt Leathes
Re: creating Scroll Events
by Matt Leathes - Tuesday, 17 April 2018, 4:38 PM
 

I'd generally just crib from an existing plugin... which one depends on why exactly you want to do as a result of the scroll event - but the bookmarking extension might be a good place to start looking?

FYI in the example code you supply you're running _.bindAll on a function ('detect_scroll') that isn't shown... and your $(window).scroll is attached to a function (this.onScroll) that isn't shown either. I'm wondering if your code should look more like this:

initialize: function() {
    $(window).scroll(this.onScroll.bind(this));
},

But without seeing those functions it's hard to know for sure...

Picture of fergus beake
Re: creating Scroll Events
by fergus beake - Wednesday, 18 April 2018, 10:22 AM
 

Hi Matt,

Thanks for the quick response.

I've manged to get the scroll function to work, thanks for your help.

 

for future reference, I ended up attaching it to the prerender like this.

preRender: function () {
     $(window).scroll(this.onScroll.bind(this));
},

I then added the function alongside the rest of the code

onScroll:function(){
   //code goes here
}
Picture of Matt Leathes
Re: creating Scroll Events
by Matt Leathes - Wednesday, 18 April 2018, 11:08 AM
 

Cool, glad I could be of help.

Just be aware - running code on 'scroll' can be hugely detrimental to performance. You should seriously consider using either _.throttle or _.debounce to restrict the amount of times your onScroll function executes.