Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Ember.js does setupController and model hooks work only for dynamic segments?

Tags:

ember.js

I am trying to understand the setupController and model hooks, will they be invoked only in case of dynamic segments?

This is my router configuration, I see the application is working fine,but I do not see these hooks getting executed:

// Router, this need to connect view and controller
App.Router.map(function(){
  this.resource("login", {path : "/"});
  this.resource("home" , {path : "home/:home_id"});
});

App.Router.IndexRoute = Ember.Route.extend({
  setupController:function(controller,model){
    console.log("in setupController hook for index route");
  }
});

App.Router.LoginRoute = Ember.Route.extend({
  setupController:function(controller,model){
    console.log("in setupController hook for login route");
  }
});

App.Router.HomeRoute = Ember.Route.extend({
  setupController:function(controller,model){
    console.log("in setupController hook for login route");
  }
});
like image 352
flash Avatar asked Mar 22 '13 12:03

flash


1 Answers

Assuming you are using the latest ember (1.0.0-RC.1) You should define your routes like this:

App.HomeRoute = Ember.Route.extend({
  ...
});

They are part of your App, and don't have the Router part.

Working JSBin example

As a note on model and setupController hook behavior:

In a route, model will only be called when navigating directly to a URL containing a dynamic segment. The parameters passed into model are used to retrieve the model for that route using the dynamic segment.

If the route is reached using a {{#linkTo route myObject}} or transitionTo(myObject) call then the passed object is used to call setupController directly and model is not called.

The setupController hook will be called every time the route enters the

Ember API docs for model

Ember API docs for setupController

like image 170
CraigTeegarden Avatar answered Sep 30 '22 13:09

CraigTeegarden