Reputation: 28910
I'm trying to add actions to an Ember.Service in Ember 1.10 but I've had to alias _actions
to the actions
hash to get it working, am I missing something?
export default Ember.Service.extend(Ember.ActionHandler, {
actions:{
addItem: function(label) {
console.log(label);
}
},
setup: Ember.on('init', function(){
this._actions = this.actions;
})
});
Looking at the ember source, the triggerEvent
method queries the _actions
hash:
if (handler._actions && handler._actions[name]) {
if (handler._actions[name].apply(handler, args) === true) {
eventWasHandled = true;
} else {
return;
}
}
I think I am missing something.
Upvotes: 2
Views: 2521
Reputation: 51
As a simpler solution and to answer dagda1, you can skip the intermediate action by setting the target on the template action. This is useful if you have a service backed component and need to maybe use the same action in various instances and want to avoid having to declare the action in the template and each respective component multiple times.
Inject the service in context:
myService: inject.service()
Target the service in your template:
{{action 'myServiceAction' target=myService}}
This will then target the actions hash on the service:
actions: {
myServiceAction() {
...
}
}
Upvotes: 1
Reputation: 23883
Actions bubble from a template into a view, then controller, then through routes. No action ever reaches a service, unless you manually do myService.send('someAction')
.
Instead of service actions, use service methods.
Just declare a method on a service, then do, for example, in a controller:
myService: Ember.inject.service(),
actions: {
someAction: function(arg) {
this.get('myService').someMethod(arg);
}
}
Upvotes: 6