Skip to content

Commit 8d024ae

Browse files
committed
Update documentation on shoebox.
1 parent 7a24827 commit 8d024ae

1 file changed

Lines changed: 45 additions & 1 deletion

File tree

README.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,51 @@ export default Ember.Route.extend({
414414

415415
Shoebox gives you great capabilities, but using it in the real app is pretty rough. Have you ever thought that such kind of logic should be done behind the scenes? In a large codebase, defining `fastboot.isFastBoot` conditionals can be a daunting task. Furthermore, it generates a lot of boilerplate code, which obscures the solution. Sooner or later coupling with `shoebox` will spread over all routes.
416416

417-
Fortunately, there is an addon called [ember-data-storefront](https://embermap.github.io/ember-data-storefront/) that can help to alleviate this pain, thanks to its Fastboot mixin: https://embermap.github.io/ember-data-storefront/docs/guides/fastboot.
417+
#### Solution: Application Adapter
418+
419+
One way to abstract the shoebox data storage mechanics is to move the logic into
420+
the Application Adapter as shown below.
421+
```
422+
export default class ApplicationAdapter extends JSONAPIAdapter.extend(
423+
...
424+
425+
cacheKeyFor([, model, id]) {
426+
return (model.modelName && id) ? `${model.modelName}-${id}` : 'default-store';
427+
}
428+
429+
async findRecord() {
430+
const key = this.cacheKeyFor(arguments);
431+
432+
if (this.fastboot.isFastBoot) {
433+
let result = await super.findRecord(...arguments);
434+
435+
// must deep-copy for clean serialization.
436+
result = JSON.parse(JSON.stringify(result));
437+
438+
this.fastboot.shoebox.put(key, result);
439+
440+
return result;
441+
}
442+
443+
let result = this.fastboot.shoebox.retrieve(key);
444+
445+
if (!result) {
446+
result = await super.findRecord(...arguments);
447+
}
448+
449+
// must deep-copy for clean serialization.
450+
return JSON.parse(JSON.stringify(result));
451+
}
452+
}
453+
```
454+
With this strategy, any time an ember-data `findRecord` request happens while in
455+
Fastboot mode, the record will be put into the shoebox cache and returned. When
456+
subsequent calls are made for that record in the hydrated application, it will
457+
first check the shoebox data.
458+
459+
#### Solution: Use an Addon (ember-storefront)
460+
461+
Additionally, there is an addon called [ember-data-storefront](https://embermap.github.io/ember-data-storefront/) that can help to alleviate this pain, thanks to its Fastboot mixin: https://embermap.github.io/ember-data-storefront/docs/guides/fastboot.
418462

419463
After installing the addon and applying the mixin, your routes can look like this:
420464

0 commit comments

Comments
 (0)