Skip to content

Commit 48ef62c

Browse files
authored
Merge pull request ember-learn#590 from muziejus/octane
ember-learn#588 Reading of "Getting Started" and "Services" Co-authored-by: Robert Jackson <[email protected]>
2 parents 41894cd + 2a1846b commit 48ef62c

File tree

2 files changed

+33
-35
lines changed

2 files changed

+33
-35
lines changed

guides/release/getting-started/quick-start.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,8 @@ To handle this function call you need to modify the `people-list` component file
249249
In the component, add an `actions` object with a `showPerson` function that alerts the first argument.
250250

251251
```javascript {data-filename="app/components/people-list.js" data-diff="+4,+5,+6,+7,+8"}
252-
import Component from '@ember/component';
252+
import Component from '@glimmer/component';
253+
import { action } from '@ember/object';
253254

254255
export default class PeopleList extends Component {
255256
@action

guides/release/services/services.md

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ Services must extend the [`Service`](https://www.emberjs.com/api/ember/release/m
2525
```javascript {data-filename=app/services/shopping-cart.js}
2626
import Service from '@ember/service';
2727

28-
export default Service.extend({
29-
});
28+
export default class ShoppingCartService extends Service {
29+
};
3030
```
3131

3232
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
3535
```javascript {data-filename=app/services/shopping-cart.js}
3636
import Service from '@ember/service';
3737

38-
export default Service.extend({
39-
items: null,
38+
export default class ShoppingCartService extends Service {
39+
items = [];
4040

41-
init() {
42-
this._super(...arguments);
43-
this.set('items', []);
44-
},
4541

4642
add(item) {
4743
this.items.pushObject(item);
48-
},
44+
}
4945

5046
remove(item) {
5147
this.items.removeObject(item);
52-
},
48+
}
5349

5450
empty() {
5551
this.items.clear();
5652
}
57-
});
53+
};
5854
```
5955

6056
### Accessing Services
@@ -67,25 +63,25 @@ When no arguments are passed, the service is loaded based on the name of the var
6763
You can load the shopping cart service with no arguments like below.
6864

6965
```javascript {data-filename=app/components/cart-contents.js}
70-
import Component from '@ember/component';
66+
import Component from '@glimmer/component';
7167
import { inject as service } from '@ember/service';
7268

73-
export default Component.extend({
69+
export default class CartContentsComponent extends Component {
7470
//will load the service in file /app/services/shopping-cart.js
75-
shoppingCart: service()
76-
});
71+
@service shoppingCart;
72+
};
7773
```
7874

7975
Another way to inject a service is to provide the name of the service as the argument.
8076

8177
```javascript {data-filename=app/components/cart-contents.js}
82-
import Component from '@ember/component';
78+
import Component from '@glimmer/component';
8379
import { inject as service } from '@ember/service';
8480

85-
export default Component.extend({
81+
export default class CartContentsComponent extends Component {
8682
//will load the service in file /app/services/shopping-cart.js
87-
cart: service('shopping-cart')
88-
});
83+
@service('shopping-cart') cart;
84+
};
8985
```
9086

9187
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,
9591
you must look up the service using Ember's [`getOwner`](https://emberjs.com/api/ember/release/classes/@ember%2Fapplication/methods/getOwner?anchor=getOwner) instead.
9692

9793
```javascript {data-filename=app/components/cart-contents.js}
98-
import Component from '@ember/component';
94+
import Component from '@glimmer/component';
9995
import { computed } from '@ember/object';
10096
import { getOwner } from '@ember/application';
10197

102-
export default Component.extend({
98+
export default class CartContentsComponent extends Component {
10399
//will load the service in file /app/services/shopping-cart.js
104-
cart: computed(function() {
100+
@computed
101+
get cart() {
105102
return getOwner(this).lookup('service:shopping-cart');
106-
})
107-
});
103+
}
104+
};
108105
```
109106

110107
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.
114111
Below we add a remove action to the `cart-contents` component.
115112

116113
```javascript {data-filename=app/components/cart-contents.js}
117-
import Component from '@ember/component';
114+
import Component from '@glimmer/component';
118115
import { inject as service } from '@ember/service';
116+
import { action } from '@ember/object';
119117

120-
export default Component.extend({
121-
cart: service('shopping-cart'),
118+
export default class CartContentsComponent extends Component {
119+
@service('shopping-cart') cart;
122120

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+
};
129126
```
130127
Once injected into a component, a service can also be used in the template.
131128
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.
135132
{{#each this.cart.items as |item|}}
136133
<li>
137134
{{item.name}}
138-
<button {{action "remove" item}}>Remove</button>
135+
<button {{action this.remove item}}>Remove</button>
139136
</li>
140137
{{/each}}
141138
</ul>

0 commit comments

Comments
 (0)