@@ -25,8 +25,8 @@ Services must extend the [`Service`](https://www.emberjs.com/api/ember/release/m
25
25
``` javascript {data-filename=app/services/shopping-cart.js}
26
26
import Service from ' @ember/service' ;
27
27
28
- export default Service . extend ( {
29
- }) ;
28
+ export default class ShoppingCartService extends Service {
29
+ };
30
30
```
31
31
32
32
Like any Ember object, a service is initialized and can have properties and methods of its own.
@@ -35,26 +35,22 @@ Below, the shopping cart service manages an items array that represents the item
35
35
``` javascript {data-filename=app/services/shopping-cart.js}
36
36
import Service from ' @ember/service' ;
37
37
38
- export default Service . extend ( {
39
- items: null ,
38
+ export default class ShoppingCartService extends Service {
39
+ items = [];
40
40
41
- init () {
42
- this ._super (... arguments );
43
- this .set (' items' , []);
44
- },
45
41
46
42
add (item ) {
47
43
this .items .pushObject (item);
48
- },
44
+ }
49
45
50
46
remove (item ) {
51
47
this .items .removeObject (item);
52
- },
48
+ }
53
49
54
50
empty () {
55
51
this .items .clear ();
56
52
}
57
- }) ;
53
+ };
58
54
```
59
55
60
56
### Accessing Services
@@ -67,25 +63,25 @@ When no arguments are passed, the service is loaded based on the name of the var
67
63
You can load the shopping cart service with no arguments like below.
68
64
69
65
``` javascript {data-filename=app/components/cart-contents.js}
70
- import Component from ' @ember /component' ;
66
+ import Component from ' @glimmer /component' ;
71
67
import { inject as service } from ' @ember/service' ;
72
68
73
- export default Component . extend ( {
69
+ export default class CartContentsComponent extends Component {
74
70
// will load the service in file /app/services/shopping-cart.js
75
- shoppingCart : service ()
76
- }) ;
71
+ @ service shoppingCart;
72
+ };
77
73
```
78
74
79
75
Another way to inject a service is to provide the name of the service as the argument.
80
76
81
77
``` javascript {data-filename=app/components/cart-contents.js}
82
- import Component from ' @ember /component' ;
78
+ import Component from ' @glimmer /component' ;
83
79
import { inject as service } from ' @ember/service' ;
84
80
85
- export default Component . extend ( {
81
+ export default class CartContentsComponent extends Component {
86
82
// will load the service in file /app/services/shopping-cart.js
87
- cart : service (' shopping-cart' )
88
- }) ;
83
+ @ service (' shopping-cart' ) cart;
84
+ };
89
85
```
90
86
91
87
This injects the shopping cart service into the component and makes it available as the ` cart ` property.
@@ -95,16 +91,17 @@ Since normal injection will throw an error if the service doesn't exist,
95
91
you must look up the service using Ember's [ ` getOwner ` ] ( https://emberjs.com/api/ember/release/classes/@ember%2Fapplication/methods/getOwner?anchor=getOwner ) instead.
96
92
97
93
``` javascript {data-filename=app/components/cart-contents.js}
98
- import Component from ' @ember /component' ;
94
+ import Component from ' @glimmer /component' ;
99
95
import { computed } from ' @ember/object' ;
100
96
import { getOwner } from ' @ember/application' ;
101
97
102
- export default Component . extend ( {
98
+ export default class CartContentsComponent extends Component {
103
99
// will load the service in file /app/services/shopping-cart.js
104
- cart: computed (function () {
100
+ @computed
101
+ get cart () {
105
102
return getOwner (this ).lookup (' service:shopping-cart' );
106
- })
107
- }) ;
103
+ }
104
+ };
108
105
```
109
106
110
107
Injected properties are lazy loaded; meaning the service will not be instantiated until the property is explicitly called.
@@ -114,18 +111,18 @@ Once loaded, a service will persist until the application exits.
114
111
Below we add a remove action to the ` cart-contents ` component.
115
112
116
113
``` javascript {data-filename=app/components/cart-contents.js}
117
- import Component from ' @ember /component' ;
114
+ import Component from ' @glimmer /component' ;
118
115
import { inject as service } from ' @ember/service' ;
116
+ import { action } from ' @ember/object' ;
119
117
120
- export default Component . extend ( {
121
- cart : service (' shopping-cart' ),
118
+ export default class CartContentsComponent extends Component {
119
+ @ service (' shopping-cart' ) cart;
122
120
123
- actions: {
124
- remove (item ) {
125
- this .cart .remove (item);
126
- }
127
- }
128
- });
121
+ @action
122
+ remove (item ) {
123
+ this .cart .remove (item);
124
+ }
125
+ };
129
126
```
130
127
Once injected into a component, a service can also be used in the template.
131
128
Note ` cart ` being used below to get data from the cart.
@@ -135,7 +132,7 @@ Note `cart` being used below to get data from the cart.
135
132
{{#each this.cart.items as |item|}}
136
133
<li>
137
134
{{item.name}}
138
- <button {{action " remove" item}}>Remove</button>
135
+ <button {{action this. remove item}}>Remove</button>
139
136
</li>
140
137
{{/each}}
141
138
</ul>
0 commit comments