Is there services concept in emberjs like angularjs

plz briefly tell me the concept of services like angularjs…

Yes, you can use services. Here's an example of a very simple service that passes some data to the server. I'm using Ember-Data but for some things i need to bypass that. This is also using Ember-CLI.

// app/services/token.js
import Ember from 'ember';
import {request} from 'ic-ajax';
import ENV from "../config/environment";

export default Ember.Service.extend({

  fetch: function(data) {
    return request({
      url       : ENV.tokenEndpoint,
      type      : "POST",
      dataType  : "json",
      data      : data,
      headers   : {"X-FooBar": 'foo'}
    });
  }
});

You need to now tell Ember that the service exists, and to which parts of the app it should be exposed.

// app/intializers/token.js
export function initialize(container, application) {
  application.inject('route:login', 'tokenService', 'service:token');
}

export default {
  name: 'tokenService',
  initialize: initialize
};

Now refer to the service as a member of the route.

// app/routes/login.js
this.tokenService.fetch(exchangeData).then(function(response) {...});
1 Like

There is an easier way to inject services now (introduced two or three minor revisions ago):

for example, into a controller:

export default Ember.Controller.extend({
  fooService: Ember.inject.service('foo') // looks up the service from app/services/foo.js
});

@Samsinite right you are! Also, instead of referring to the service with this.tokenService we’d need to use this.get(‘tokenService’) as i understand. And we can use Ember.inject inside of any object: controller, route, etc.