Reputation: 5308
I have route map like below.
App.Router.map(function() {
this.resource('category', function() {
this.resource('field', { path: ':field_id' });
});
});
Whenever a field route changed, i need to call a function in FieldController. Is there a way to do this?
init
function in Controller get called only once.
Upvotes: 2
Views: 1303
Reputation: 651
You have two options to do what you want and they both use the setupController hook of the Route class.
First method is to do wathever you want to do in the setupController function, like so:
setupController: function(controller, model){
alert('myAlert');
}
Second method is to call the controller's method from the setupController hook of your route. I did a fiddle to show how it works: http://jsfiddle.net/NQKvy/237/
Both method will fire the function when the URL changes. If you want to do fire a function while staying in the same URL, then it's completely different.
Let me know if the fiddle answers your question.
Upvotes: 3
Reputation: 19128
Like @knikolov have said you can use the afterModel
to be notified when is transitioned to the FieldRoute
. To get a FieldController
instance you can use the this.controllerFor('field')
:
App.FieldRoute = Ember.Route.extend({
afterModel: function() {
this.controllerFor('field').myMethod();
}
});
App.FieldController = Ember.ObjectController.extend({
myMethod: function() {
alert('myMethod');
}
});
Here is a fiddle with this sample http://jsfiddle.net/marciojunior/Q5XWg/
Upvotes: 3