An Ember.Service is an Ember object that lives for the duration of the application, and can be made available in different parts of your application.
An Ember.Service is an Ember object that lives for the duration of the application, and can be made available in different parts of your application.
Services are useful for features that require shared state or persistent connections.
$ ember g service session
import Ember from 'ember';
export default Ember.Service.extend({
name: 'jupe',
greeting: Ember.computed('name', function () {
return `Hello, $(this.get('name'))`;
}),
fetchStuff () {
// this could be async
return [];
}
});
session: Ember.inject.service()
// available in this class as this.get('session');
Or
userSession: Ember.inject.service('session')
// now available in this class as this.get('userSession');
// app/initializers/session.js
export function initialize(application) {
application.inject('route', 'session', 'service:session');
}
export default {
name: 'session',
initialize: initialize
};
// now available in all routes as this.get('session');
Re-usable bits of ui, much like web components
Composed of a js file and/or a template
Isolated - do not have access to their outer context, get data & actions via attributes
$ ember g component user-profile
// app/components/user-profile/component.js
import Ember from 'ember';
export default Ember.Component.extend({
tagName: 'h2',
classNames: [ 'user-profile-component' ],
greeting: Ember.computed('firstName', 'lastName', function () {
return `Hello, ${this.get('firstName')} ${this.get('lastName')}`;
})
});
{{!-- app/components/user-profile/template.hbs --}}
{{greeting}}
{{!-- some template somewhere --}}
{{user-profile firstName="Mike" lastName="Juniper"}}
this._super(...arguments);
this.$(...);
Instead they: