Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emberjs scroll to top when changing view

Tags:

When the main view of my application is switched (new route that reconnects the main outlet of my application controller) I want the page to be scrolled to the top. Otherwise it's a bit strange that I navigate to another page-like view and the viewport is still lost somewhere where I left off.

I hacked a solution and wonder if there's a better way or if anyone has the same thing.

Here's what I do:

App.ApplicationController = Ember.Controller.extend({   connectOutlet: function(){     window.scrollTo(0, 0);     this._super.apply(this, arguments);   } }); 
like image 687
xMartin Avatar asked Oct 29 '12 11:10

xMartin


1 Answers

@Baruch's solution is good, but when I implemented it I had render on elements within my application state and would cause a scrollTop when it was not needed.

I found this to be much more effective as it only runs on the path change:

App.ApplicationController = Ember.Controller.extend({    currentPathChanged: function () {     window.scrollTo(0, 0);   }.observes('currentPath')  }); 
like image 90
Michael Benin Avatar answered Sep 18 '22 17:09

Michael Benin