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");
},
})
});
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With