2
votes

How can I call a function inside action handlebars from setupController located in the Route. (Also, how can I call a function inside a controller and not the route itself).

I want my page to analyse the URL parameters and fill in some variables based on those parameters.

App.CresRoute = Ember.Route.extend({
  setupController: function(){
    this.send('testFunction', "Print me");
  },

  actions: {
    testFunction: function(string){
        console.log(string);
    },
  }
});

This returns an error: "Nothing handled the function 'testFunction'."

Obviously my methods are more complex, with many parameters, and this is just to demonstrate the problem.

2

2 Answers

0
votes

This should work for you.

App.CresRoute = Ember.Route.extend({
  setupController: function(){
    this.actions.testFunction('Print me');
  },

  actions: {
    testFunction(string){
        console.log(string);
    },
  }
});
0
votes

I've edited this to try to clarify your question, but I'm not sure what you mean by "call a function inside action handlebars from setupController".

In the sample code you've included, for testFunction to respond, it would need to be higher up the route tree from where it's called. Actions go up. If you're trying to set variables on the controller, you're right to be doing it in the setupController hook, but you don't need actions to do it; just define testFunction outside the actions block, and call it like a normal method on your route:

App.CresRoute = Ember.Route.extend({
  setupController: function(){
    this.testFunction("Print me");
  },

  testFunction: function(string){
    console.log(string);
  }
});

I don't know what Handlebars has to do with this question, so I wonder if I've understood properly what you're trying to do?