0
votes

Is it possible to call another controllers method's from a handlebars template?

<script type="text/x-handlebars" data-template-name="home">
    <a {{action logout target="Application.loginController"}}>
        logout
    </a>
</script>

Error

Uncaught TypeError: Cannot read property 'send' of undefined ember.js:25757
    (anonymous function) ember.js:25757
    (anonymous function) ember.js:4509
    Ember.handleErrors ember.js:411
    invoke ember.js:4507
    tryable ember.js:4692
    Ember.tryFinally ember.js:1206
    Ember.run ember.js:4696
    ActionHelper.registeredActions.(anonymous function).handler ember.js:25756
    (anonymous function) ember.js:14452
    Ember.handleErrors ember.js:411
    (anonymous function) ember.js:14444
    b.event.dispatch jquery.js:3
    v.handle jquery.js:3

Environment

Browser: Chrome Version 26.0.1410.63

Ember: 1.0.0-rc.3

Handlebars: 1.0.0-rc.3

Jquery: 1.9.1

1
yes it's possible, can you show how you are creating you Application.loginController?intuitivepixel
The loginController is created by ember. Are you wanting to see its definition?Ben Crowhurst

1 Answers

1
votes

You can accomplish this using the needs feature of Ember.

You specify that a controller "needs" to access another controller and then Ember allows you to access it at controllers.controllerName:

App.ApplicationController = Ember.Controller.extend({
  applicationAction: function() {
    console.log("action in application controller");
  }
});

App.IndexController = Ember.ArrayController.extend({
  needs: ['application'],
  indexProperty: "index controller"
});

This can be accessed in the template:

<a href='#' {{action applicationAction target="controllers.application"}}>
    logout
</a>

JSBin example