Why are browser scroll events not delegated through Ember?


#1

Our app calls for us to handle scroll events in a view. We had a look through the Ember source and it doesn’t appear that scroll events are part of the automatic Ember event delegation. We had to manually hook it up in our view via:

$(window).scroll(function...);

We then started to run into some issues when testing as we have disabled auto-run. We ended up working around this by wrapping the scroll handler with a run loop. This seems to work fine but would like some input from the community as to whether this is a reasonable approach and the reasoning behind scroll events not being automatically delegated.

Cheers


#2

I think we decided not to watch scroll by default because 1) they are specific to any view and 2) they can fire a whole lot. Your approach is correct, both binding to the window and wrapping in Ember.run. Make sure you do the binding in didInsertElement and tear it down in willRemoveElement.


#3

Ok. We had an inkling that performance was probably one of the main reasons.

Thanks for the feedback


#4

I’d like to see how you wrapped the scroll handler in the run loop. I’m still trying to learn how it works.


#5
// ... My class
scroll: function() {
   // You can pass this directly but i've extracted it here for clarity
   var view = this;
   Ember.run(view, function() {
      // Scroll work
   })
},
// ... The rest of the class

#6

Heads up to anyone who finds this, the binding is actually willDestroyElement.