Skip to content

Commit 36a12b7

Browse files
authored
Merge pull request #275 from ember-learn/fix-beta-6-10
Fix Tutorial for 6.10 beta
2 parents 03e28f1 + 2ce55d9 commit 36a12b7

File tree

3 files changed

+40
-42
lines changed

3 files changed

+40
-42
lines changed

.local.dic

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@ yay
3636
EmberData
3737
RequestManager
3838
centric
39+
WarpDrive

src/markdown/tutorial/part-1/01-orientation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ As text on the welcome page pointed out, the source code for the page is located
191191
- {{outlet}}
192192
-
193193
- {{! The following component displays Ember's default welcome message. }}
194-
- <WelcomePage />
194+
- <WelcomePage @extension="gjs" />
195195
- {{! Feel free to remove this! }}
196196
+ Hello World!!!
197197
</template>

src/markdown/tutorial/part-2/11-ember-data.md

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ Chances are, as we keep working on this app, we will need to add more routes tha
3434

3535
Fortunately, we're not going to do any of that. As it turns out, there's a much better solution here: we can use EmberData! As its name implies, [EmberData](../../../models/) is a library that helps manage data and *[application state][TODO: link to application state]* in Ember applications.
3636

37-
There's a lot to learn about EmberData, but let's start by uncovering features that help with our immediate problem.
3837

3938
> Zoey says...
4039
>
41-
> RequestManager is available starting with the EmberData 4.12 LTS release. EmberData works with multiple versions of Ember, please refer to the Compatibility section of the [EmberData README](https://github.com/emberjs/data/blob/main/README.md#compatibility) while doing your application upgrade.
40+
> EmberData is in the process of being renamed to WarpDrive. It's not just changing its name though, WarpDrive is now able to be used in multiple different frameworks. You can read more about it in the [WarpDrive Docs](https://warp-drive.io)
41+
42+
There's a lot to learn about EmberData, but let's start by uncovering features that help with our immediate problem.
4243

4344
## EmberData Models
4445

@@ -47,7 +48,7 @@ EmberData is built around the idea of organizing your app's data into *[model ob
4748
Enough talking, why don't we give that a try!
4849

4950
```run:file:create lang=js cwd=super-rentals filename=app/models/rental.js
50-
import Model, { attr } from '@ember-data/model';
51+
import Model, { attr } from '@warp-drive/legacy/model';
5152

5253
const COMMUNITY_CATEGORIES = ['Condo', 'Townhouse', 'Apartment'];
5354

@@ -71,7 +72,7 @@ export default class RentalModel extends Model {
7172
}
7273
```
7374

74-
Here, we created a `RentalModel` class that extends EmberData's `Model` superclass. When fetching the listing data from the server, each individual rental property will be represented by an instance (also known as a *[record](../../../models/finding-records/)*) of our `RentalModel` class.
75+
Here, we created a `RentalModel` class that extends EmberData's `Model` superclass (which is imported from the `@warp-drive/legacy` package). When fetching the listing data from the server, each individual rental property will be represented by an instance (also known as a *[record](../../../models/finding-records/)*) of our `RentalModel` class.
7576

7677
We used the `@attr` decorator to declare the attributes of a rental property. These attributes correspond directly to the `attributes` data we expect the server to provide in its responses:
7778

@@ -151,17 +152,32 @@ The generator created some boilerplate code for us, which serves as a pretty goo
151152
152153
This model test is also known as a *[unit test](../../../testing/testing-models/)*. Unlike any of the other tests that we've written thus far, this test doesn't actually *render* anything. It just instantiates the rental model object and tests the model object directly, manipulating its attributes and asserting their value.
153154
154-
It is worth pointing out that EmberData provides a `store` *[service](../../../services/)*, also known as the EmberData store. In our test, we used the `this.owner.lookup('service:store')` API to get access to the EmberData store. The store provides a `createRecord` method to instantiate our model object for us. To make this `store` service available, we must add the following file:
155+
It is worth pointing out that EmberData provides a `store` *[service](../../../services/)*, also known as the EmberData store. In our test, we used the `this.owner.lookup('service:store')` API to get access to the EmberData store. The store provides a `createRecord` method to instantiate our model object for us.
156+
157+
This is the store that is generated for us as part of the default blueprint:
158+
159+
```js { data-filename="app/services/store.js" }
160+
import { useLegacyStore } from '@warp-drive/legacy';
161+
import { JSONAPICache } from '@warp-drive/json-api';
155162

156-
```run:file:create lang=js cwd=super-rentals filename=app/services/store.js
157-
export { default } from 'ember-data/store';
163+
const Store = useLegacyStore({
164+
linksMode: false,
165+
cache: JSONAPICache,
166+
handlers: [
167+
// -- your handlers here
168+
],
169+
schemas: [
170+
// -- your schemas here
171+
],
172+
});
173+
174+
export default Store;
158175
```
159176
160177
Running the tests in the browser confirms that everything is working as intended:
161178
162179
```run:command hidden=true cwd=super-rentals
163180
pnpm test
164-
git add app/services/store.js
165181
git add tests/unit/models/rental-test.js
166182
```
167183
@@ -180,7 +196,7 @@ Alright, now that we have our model set up, it's time to refactor our route hand
180196
-
181197
-const COMMUNITY_CATEGORIES = ['Condo', 'Townhouse', 'Apartment'];
182198
+import { service } from '@ember/service';
183-
+import { query } from '@ember-data/json-api/request';
199+
+import { query } from '@warp-drive/utilities/json-api';
184200

185201
export default class IndexRoute extends Route {
186202
- async model() {
@@ -212,7 +228,7 @@ Alright, now that we have our model set up, it's time to refactor our route hand
212228
-
213229
-const COMMUNITY_CATEGORIES = ['Condo', 'Townhouse', 'Apartment'];
214230
+import { service } from '@ember/service';
215-
+import { findRecord } from '@ember-data/json-api/request';
231+
+import { findRecord } from '@warp-drive/utilities/json-api';
216232

217233
export default class RentalRoute extends Route {
218234
- async model(params) {
@@ -242,9 +258,9 @@ Wow... that removed a lot of code! This is all possible thanks to the power of c
242258
243259
## The EmberData Store
244260
245-
As mentioned above, EmberData provides a `store` service, which we can inject into our route using the `@service store;` declaration, making the EmberData store available as `this.store`. It provides the `request` method for making fetch requests using `RequestManager`. As its name implies: the `RequestManager` is request centric. Instead of answering questions about specific records or types of records, we ask it about the status of a specific request. To initiate a request, we use the `request` method on the store, passing in a request object. The request object is created using builders from `@ember-data/json-api/request`. Specifically, the [`findRecord` builder](../../../models/finding-records/#toc_retrieving-a-single-record) takes a model type (`rental` in our case) and a model ID (for us, that would be `params.rental_id` from the URL) as arguments and builds fetch options for a single record. On the other hand, the [`query` builder](../../../models/finding-records/#toc_retrieving-multiple-records) takes the model type as an argument and builds fetch options to query for all records of that type.
261+
As mentioned above, EmberData provides a `store` service, which we can inject into our route using the `@service store;` declaration, making the EmberData store available as `this.store`. It provides the `request` method for making fetch requests using `RequestManager`. As its name implies: the `RequestManager` is request centric. Instead of answering questions about specific records or types of records, we ask it about the status of a specific request. To initiate a request, we use the `request` method on the store, passing in a request object. The request object is created using builders from `@warp-drive/utilities/json-api`. Specifically, the [`findRecord` builder](../../../models/finding-records/#toc_retrieving-a-single-record) takes a model type (`rental` in our case) and a model ID (for us, that would be `params.rental_id` from the URL) as arguments and builds fetch options for a single record. On the other hand, the [`query` builder](../../../models/finding-records/#toc_retrieving-multiple-records) takes the model type as an argument and builds fetch options to query for all records of that type.
246262
247-
EmberData can do many things, and in default setup it provides caching. EmberData's store caches server responses, allowing instant access to previously fetched data. If the data is already cached, you don't need to wait for the server to respond again. If not, the store fetches it for you.
263+
EmberData can do many things, and in the default setup it provides caching. EmberData's store caches server responses, allowing instant access to previously fetched data. If the data is already cached, you don't need to wait for the server to respond again. If not, the store fetches it for you.
248264
249265
That's a lot of theory, but is this going to work in our app? Let's run the tests and find out!
250266
@@ -273,7 +289,7 @@ The first thing we want to do is have our builder respect a configurable default
273289
274290
```run:file:patch lang=js cwd=super-rentals filename=app/app.js
275291
@@ -7,0 +8,5 @@ import setupInspector from '@embroider/legacy-inspector-support/ember-source-4.1
276-
+import { setBuildURLConfig } from '@ember-data/request-utils';
292+
+import { setBuildURLConfig } from '@warp-drive/utilities/json-api';
277293
+
278294
+setBuildURLConfig({
279295
+ namespace: 'api',
@@ -297,35 +313,17 @@ export const JsonSuffixHandler = {
297313
298314
As you can see, the handler appends `.json` to the URL of each request. Pretty simple, right? Then it calls the `next` function with the modified copy of the request object (because it is immutable). This is how we can chain multiple handlers together to build up a request.
299315
300-
The next step that we need to do, is to configure `RequestManager` to use this handler. Let's create the request-manager service.
301-
302-
```run:file:create lang=js cwd=super-rentals filename=app/services/request-manager.js
303-
import BaseRequestManager from '@ember-data/request';
304-
import Fetch from '@ember-data/request/fetch';
305-
import { JsonSuffixHandler } from 'super-rentals/utils/handlers';
306-
307-
export default class RequestManager extends BaseRequestManager {
308-
constructor(args) {
309-
super(args);
310-
311-
this.use([JsonSuffixHandler, Fetch]);
312-
}
313-
}
314-
```
315-
316-
Notice that we are using the `JsonSuffixHandler` we created earlier. We also use the `Fetch` handler, which is a built-in handler that makes the actual fetch request. The `use` method is used to add handlers to the request manager. The order in which handlers are added is important, as they will be executed in the order they were added.
317-
318-
Lastly, let's update our `store` service to use the new `RequestManager` we created.
316+
The next step that we need to do, is to configure our `legacyStore` to use this handler. Let's update the store service to add this handler:
319317

320318
```run:file:patch lang=js cwd=super-rentals filename=app/services/store.js
321-
@@ -1 +1,6 @@
322-
-export { default } from 'ember-data/store';
323-
+import BaseStore from 'ember-data/store';
324-
+import { service } from '@ember/service';
325-
+
326-
+export default class Store extends BaseStore {
327-
+ @service requestManager;
328-
+}
319+
@@ -2,2 +2,3 @@ import { useLegacyStore } from '@warp-drive/legacy';
320+
import { JSONAPICache } from '@warp-drive/json-api';
321+
+import { JsonSuffixHandler } from 'super-rentals/utils/handlers';
322+
323+
@@ -8,2 +9,3 @@ const Store = useLegacyStore({
324+
// -- your handlers here
325+
+ JsonSuffixHandler
326+
],
329327
```
330328

331329
With our new EmberData configuration in place, all our tests should pass again.
@@ -335,7 +333,6 @@ pnpm test
335333
git add app/app.js
336334
git add app/routes/index.js
337335
git add app/routes/rental.js
338-
git add app/services/request-manager.js
339336
git add app/services/store.js
340337
git add app/utils/handlers.js
341338
```

0 commit comments

Comments
 (0)