Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obversing route change to apply equivalent to onload

I'm trying to observe the route change to apply some common action once rendered. The idea is to have a feature similar to the onload but as we use a single-page app this needs to be triggered on each route changes. (could be scoped to the new view)

I found how to observe the currentPath changes:

App.ApplicationController = Ember.Controller.extend({
  currentPathDidChange: function() {
    prettyPrint()
  }.observes('currentPath');
});

While this works good in some cases, it gets triggered when the route changes, but still to early to apply content changes as it seem to append before the content gets rendered.

Any idea on the best practice to achieve such goal?

like image 584
Sébastien Grosjean - ZenCocoon Avatar asked Apr 15 '13 19:04

Sébastien Grosjean - ZenCocoon


People also ask

Is there any difference between body onload () and document ready () function?

The main differences between the two are: Body. Onload() event will be called only after the DOM and associated resources like images got loaded, but jQuery's document. ready() event will be called once the DOM is loaded i.e., it wont wait for the resources like images to get loaded.

What is the difference between the window onload event and the Ondocumentready event?

ready() is a jQuery event which occurs when the HTML document has been fully loaded, while the window. onload event occurs later, when everything including images on the page loaded.

Is window onload deprecated?

Note: The load() method deprecated in jQuery version 1.8. It was completely removed in version 3.0.

What's the difference between window onload vs document onload?

The general idea is that window. onload fires when the document's window is ready for presentation and document. onload fires when the DOM tree (built from the markup code within the document) is completed.


2 Answers

Have you tried deferring the code with Ember.run.schedule? For instance,

App.ApplicationController = Ember.Controller.extend({
  currentPathDidChange: function() {
    Ember.run.schedule('afterRender', this, function() {
      prettyPrint();
    });
  }.observes('currentPath')
});
like image 145
Jo Liss Avatar answered Nov 15 '22 10:11

Jo Liss


Due to the deprecation of Controllers in Ember 1.x finding the url in the router would be a good way to future proof your apps. You can do this in ember-cli like so:

// similar to onLoad event behavior
export default Ember.Route.extend({
  afterModel: function (model){
    Ember.run.next(() => {
      console.log(this.get('router.url'));
    });
  }
});

// hacky way to get current url on each transition
export default Ember.Route.extend({
  actions: {
    didTransition: function() {
      Ember.run.next(() => {
        console.log(this.get('router.url'));
      });
    }
  }
});

This will log: /posts and /posts/3/comments ect.

like image 31
Matt Jensen Avatar answered Nov 15 '22 10:11

Matt Jensen