1

What benefits does an ember service class offer over other stereotypes like utils? I think it's related to DI, but I can't quite put my finger on it.

leojh
  • 7,160
  • 5
  • 28
  • 31
  • I don't think this question is a very good fit for the site, but to give you my opinion, it's all about dependency injection and inversion of control. You can read more about those [here](http://stackoverflow.com/questions/3058/what-is-inversion-of-control). – GJK May 15 '15 at 15:31
  • I know what DI is use it every day... my question pertains only to the new stereotype of Service classes released in ember recently. – leojh May 15 '15 at 17:45
  • I don't understand what you're asking then. You wanted to know why one would use a service instead of a utility module and dependency injection _is_ the answer. Services use dependency injection, utility modules do not. That's really all there is to it. – GJK May 15 '15 at 18:37
  • I'd recommend to ask it on [the discussion board](http://discuss.emberjs.com) – Tim Tonkonogov May 16 '15 at 01:20

1 Answers1

2

The primary advantage is that you can use services from other Ember objects by using Ember.inject.service().

For example, if you have a user service, you could do:

// app/components/user.js export default Ember.Component.extend({ userService: Ember.inject.service('user'), displayName: Ember.computed('userService.model.{firstName,lastName}', function(){ return [ this.get('userService.model.firstName'), this.get('userService.model.lastName') ].compact().join(' '); }), avatarUrl: Ember.computed.readOnly('userService.model.avatarUrl'), });

Luke Melia
  • 8,389
  • 33
  • 41