Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember.js Router Action to Controller

When I use the Ember Router, how can I define actions in the template who are connected to the controller?

An Example is here: http://jsfiddle.net/KvJ38/3/

Unter My Profile are two actions: One is defined on the State, and is working Two is defined on the Controller. How can i make this working or should I use another approach?

App.Router = Em.Router.extend({
  enableLogging: true,
  location: 'hash',

  root: Em.State.extend({
    // EVENTS
    goHome: Ember.State.transitionTo('home'),
    viewProfile: Ember.State.transitionTo('profile'),

    // STATES
    home: Em.State.extend({
      route: '/',
      connectOutlets: function(router, context) {
        var appController = router.get('applicationController');
        appController.connectOutlet(App.HomeView);
      }
     }),

    // STATES
    profile: Em.State.extend({
      route: '/profile',
        connectOutlets: function(router, context) {
          var appController = router.get('applicationController');
          appController.connectOutlet(App.ProfileView);
        }
    }),

    one: function() {
      alert("eins");
    },
  }) 
});
like image 388
Lux Avatar asked Jun 15 '12 12:06

Lux


People also ask

How do I use a controller with Ember JS?

Defining a Controller We only need to generate a Controller file if we want to customize the properties or provide any actions to the Route. If we have no customizations, Ember will provide a default Controller instance for us at run time. This creates a controller file at app/controllers/my-controller-name.

What is Route action in Ember?

ember install ember-route-action-helper. The route-action helper allows you to bubble closure actions, which will delegate it to the currently active route hierarchy per the bubbling rules explained under actions . Like closure actions, route-action will also have a return value.

What is a routing controller?

Routing Controllers is a framework for building backend applications in Node. It is quite lightweight, easy to learn and pleasant to use. You can find their repo at https://github.com/typestack/routing-controllers.

What is index route in Ember?

Index Routes. At every level of nesting (including the top level), Ember automatically provides a route for the / path named index . To see when a new level of nesting occurs, check the router, whenever you see a function , that's a new level. For example, if you write a simple router like this: app/router.js Router.


1 Answers

The default target of an action is the router, but you can define another one in the template:

{{action two target="controller"}}

And add a "two" function in "App.ProfileController".

UPDATE

This answer was hopefully correct mid 2012. Now (September 2014), the documentation says:

By default, the {{action}} helper triggers a method on the template's controller. [...] If the controller does not implement a method with the same name as the action in its actions object, the action will be sent to the router, where the currently active leaf route will be given a chance to handle the action. [...] If neither the template's controller nor the currently active route implements a handler, the action will continue to bubble to any parent routes. Ultimately, if an ApplicationRoute is defined, it will have an opportunity to handle the action. When an action is triggered, but no matching action handler is implemented on the controller, the current route, or any of the current route's ancestors, an error will be thrown.

like image 82
Stéphane Blond Avatar answered Sep 19 '22 10:09

Stéphane Blond