Skip to content

Commit 67f07ae

Browse files
authored
Merge pull request #762 from burritoIand/master
Update documentation on shoebox.
2 parents 7a24827 + 6cd0964 commit 67f07ae

1 file changed

Lines changed: 46 additions & 1 deletion

File tree

README.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,52 @@ 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+
```
423+
export default class ApplicationAdapter extends JSONAPIAdapter.extend(
424+
// ...snip...
425+
426+
cacheKeyFor([, model, id]) {
427+
return (model.modelName && id) ? `${model.modelName}-${id}` : 'default-store';
428+
}
429+
430+
async findRecord() {
431+
const key = this.cacheKeyFor(arguments);
432+
433+
if (this.fastboot.isFastBoot) {
434+
let result = await super.findRecord(...arguments);
435+
436+
// must deep-copy for clean serialization.
437+
result = JSON.parse(JSON.stringify(result));
438+
439+
this.fastboot.shoebox.put(key, result);
440+
441+
return result;
442+
}
443+
444+
let result = this.fastboot.shoebox.retrieve(key);
445+
446+
if (!result) {
447+
result = await super.findRecord(...arguments);
448+
}
449+
450+
// must deep-copy for clean serialization.
451+
return JSON.parse(JSON.stringify(result));
452+
}
453+
}
454+
```
455+
With this strategy, any time an ember-data `findRecord` request happens while in
456+
Fastboot mode, the record will be put into the shoebox cache and returned. When
457+
subsequent calls are made for that record in the hydrated application, it will
458+
first check the shoebox data.
459+
460+
#### Solution: Use an Addon (ember-storefront)
461+
462+
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.
418463

419464
After installing the addon and applying the mixin, your routes can look like this:
420465

0 commit comments

Comments
 (0)