Skip to content

Commit 9b7ad14

Browse files
Update ember-resources chapters (#1732)
* Update prose.md * Update prose / answer * update
1 parent 17047e3 commit 9b7ad14

File tree

4 files changed

+39
-24
lines changed

4 files changed

+39
-24
lines changed

apps/tutorial/public/docs/2-reactivity/6-resource-builders/prose.md

+3
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,7 @@ It is:
3737
<time>{{Clock 'ja-JP-u-ca-japanese'}}</time><br />
3838
```
3939

40+
Learn more about [resourceFactory here](https://github.com/NullVoxPopuli/ember-resources/blob/main/docs/docs/what-is-resourceFactory.md).
41+
42+
4043
[mdn-DateTimeFormat]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat

apps/tutorial/public/docs/2-reactivity/6-resource-composition/answer.gjs

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import { resource, cell, resourceFactory } from 'ember-resources';
22

3-
const Time = resourceFactory((ms) => resource(({ on }) => {
4-
let time = cell(Date.now());
5-
let interval = setInterval(() => time.current = Date.now(), ms);
3+
function Time(ms) {
4+
return resource(({ on }) => {
5+
let time = cell(Date.now());
6+
let interval = setInterval(() => time.current = Date.now(), ms);
67

7-
on.cleanup(() => clearInterval(interval));
8+
on.cleanup(() => clearInterval(interval));
89

9-
return time;
10-
}));
10+
return time;
11+
});
12+
}
13+
resourceFactory(Time);
1114

1215
const Clock = resource(({ use }) => {
1316
return use(Time(1_000));
@@ -18,7 +21,7 @@ const Stopwatch = resource(({ use }) => {
1821
});
1922

2023
// from a previous example
21-
const FormattedClock = resourceFactory((locale = 'en-US') => {
24+
function FormattedClock(locale = 'en-US') {
2225
let formatter = new Intl.DateTimeFormat(locale, {
2326
month: 'long',
2427
day: 'numeric',
@@ -33,7 +36,8 @@ const FormattedClock = resourceFactory((locale = 'en-US') => {
3336

3437
return () => formatter.format(time.current);
3538
});
36-
});
39+
}
40+
resourceFactory(FormattedClock);
3741

3842
// demonstrating that use can be used multiple times
3943
const Watch = resource(({ use }) => {

apps/tutorial/public/docs/2-reactivity/6-resource-composition/prompt.gjs

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import { resource, cell, resourceFactory } from 'ember-resources';
22

3-
const Time = resourceFactory((ms) => resource(({ on }) => {
4-
let time = cell(Date.now());
5-
let interval = setInterval(() => time.current = Date.now(), ms);
3+
function Time(ms) {
4+
return resource(({ on }) => {
5+
let time = cell(Date.now());
6+
let interval = setInterval(() => time.current = Date.now(), ms);
67

7-
on.cleanup(() => clearInterval(interval));
8+
on.cleanup(() => clearInterval(interval));
89

9-
return time;
10-
}));
10+
return time;
11+
});
12+
}
13+
resourceFactory(Time);
1114

1215
const Clock = resource(({ use }) => {
1316
/* Add your composition here */

apps/tutorial/public/docs/2-reactivity/6-resource-composition/prose.md

+15-10
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,36 @@ In previous chapters, we've built a clock, which updates every second.
44
But now let's say we also want to make a Stopwatch, but we only want to manage `setInterval` once, we may want to make a Resource with configurable interval milliseconds, like this:
55

66
```js
7-
const Time = resourceFactory((ms) => resource(({ on }) => {
8-
let time = cell(Date.now());
9-
let interval = setInterval(() => time.current = Date.now(), ms);
10-
11-
on.cleanup(() => clearInterval(interval));
12-
13-
return time;
14-
}));
7+
function Time(ms) {
8+
return resource(({ on }) => {
9+
let time = cell(Date.now());
10+
let interval = setInterval(() => time.current = Date.now(), ms);
11+
12+
on.cleanup(() => clearInterval(interval));
13+
14+
return time;
15+
})
16+
}
17+
resourceFactory(Time); // declare intent to use in a template
1518
```
19+
Learn more about [resourceFactory here](https://github.com/NullVoxPopuli/ember-resources/blob/main/docs/docs/what-is-resourceFactory.md).
1620

1721
This uses the [`Date.now()`][mdn-date] method which gives us millisecond precision and represents the time in milliseconds since January 1, 1970 00:00:00 UTC (the [epoch][ecma-epoch]).
1822

1923

2024
The `on` object is not the only property we have at our disposal. We are provided a `use` function that allows us to _use_ other resources.
2125

2226
```js
23-
const FormattedClock = resourceFactory((locale = 'en-US') => {
27+
function FormattedClock(locale = 'en-US') {
2428
let formatter = new Intl.DateTimeFormat(locale, { /* ... */ });
2529

2630
return resource(({ on, use }) => {
2731
const time = use(Time(1_000));
2832

2933
return () => formatter.format(time.current);
3034
});
31-
});
35+
}
36+
resourceFactory(FormattedClock);
3237
```
3338

3439
This allows us to use the same resource to both make a `Clock` as well as a `Stopwatch`

0 commit comments

Comments
 (0)