Data Loading
Patterns
EmberConf 2017, Portland
03/28/2017
Slide 2
Slide 2 text
Data Loading
Patterns
with JSON API
EmberConf 2017, Portland
03/28/2017
Slide 3
Slide 3 text
This talk is going to be …
• A series of prevalent use cases
• Rich in code examples
• Packed with actionable advice
Slide 4
Slide 4 text
Why JSON API?
Slide 5
Slide 5 text
What kind of API?
• Backend dev: “What kind of API are we
going to use?”
• Me: “JSON API”
• We: “Let’s start working then”
Slide 6
Slide 6 text
CoC for APIs
Slide 7
Slide 7 text
Covering The Basics
Slide 8
Slide 8 text
Covering the basics (ED)
• store.findRecord(‘band’, 1)
• if the record is in the store => returns it,
and re-fetches it in the background
• if the record is not in the store => returns
a promise
Slide 9
Slide 9 text
Covering the basics (ED)
• store.findAll(‘band’)
• if >= 1 record in the store => returns them
and triggers a refetch in the bg.
• if there is no record in the store =>
returns a promise to fetch all of them
Slide 10
Slide 10 text
Covering the basics (ED)
• store.peekRecord(‘band’, 1)
• never triggers a reload
• returns the record if it’s in the store
• returns null if it’s not
Slide 11
Slide 11 text
Covering the basics (ED)
• store.peekAll(‘band’)
• never triggers a reload
• returns the records already in the store
Slide 12
Slide 12 text
Covering the basics (ED)
• shouldBackgroundReloadRecord
• shouldBackgroundReloadAll
• configurable on the adapter
Slide 13
Slide 13 text
Covering the basics (Ember)
• The model hooks are resolved top-down
• A child route is not entered before the
hooks are run for its parent
Slide 14
Slide 14 text
Covering the basics (Ember)
• Returning a promise in the model hook
“blocks” rendering the page
• `findRecord`, `findAll`, `query` all return
promises
Slide 15
Slide 15 text
The app
Slide 16
Slide 16 text
application
bands
bands.band.songs
bands.band
Slide 17
Slide 17 text
Balint Erdi
@baaz
balinterdi.com
Ember.js consultant
Slide 18
Slide 18 text
Case Studies
Slide 19
Slide 19 text
Fetching
Relationship Data
Slide 20
Slide 20 text
Lazy Fetching
Slide 21
Slide 21 text
No content
Slide 22
Slide 22 text
No content
Slide 23
Slide 23 text
Pros & cons
• Simple, works out of the box
• On-demand data fetching
• Might trigger N requests
• Disturbing flicker
Slide 24
Slide 24 text
Pre-loading
the relationship
Slide 25
Slide 25 text
No content
Slide 26
Slide 26 text
No content
Slide 27
Slide 27 text
Move relationship data fetching
to the route’s model hook if you
want to block rendering
Slide 28
Slide 28 text
Pros & cons
• Better UX than the lazy fetching scenario
• Might still trigger N requests
• RSVP.hash in the model hook is considered an
anti-pattern
Slide 29
Slide 29 text
Side-loading
Slide 30
Slide 30 text
“An endpoint MAY also support an
include request parameter to allow
the client to customize which related
resources should be returned.”
Slide 31
Slide 31 text
No content
Slide 32
Slide 32 text
No content
Slide 33
Slide 33 text
No content
Slide 34
Slide 34 text
No content
Slide 35
Slide 35 text
Why is there a flicker? Shouldn’t
the songs be side-loaded and
only then the page rendered?
Slide 36
Slide 36 text
No content
Slide 37
Slide 37 text
No content
Slide 38
Slide 38 text
No content
Slide 39
Slide 39 text
Pros & cons
• We leverage JSON:API
• Explicit control over fetching relationship data
• Delays the rendering of the band template
Slide 40
Slide 40 text
Honorable mention:
References API
(ED 2.5)
Slide 41
Slide 41 text
Data loading strategy
should be part of the
design process
Slide 42
Slide 42 text
Searching
on the
Backend
Slide 43
Slide 43 text
“The filter query parameter is
reserved for filtering data.
Servers and clients SHOULD use
this key for filtering operations.”
Slide 44
Slide 44 text
No content
Slide 45
Slide 45 text
No content
Slide 46
Slide 46 text
Does a global search
Slide 47
Slide 47 text
No content
Slide 48
Slide 48 text
No content
Slide 49
Slide 49 text
Use a JSON API filter
and query the
relationship
Slide 50
Slide 50 text
Sorting
Slide 51
Slide 51 text
“An endpoint MAY support requests
to sort the primary data with a sort
query parameter. The value for sort
MUST represent sort fields.”
Slide 52
Slide 52 text
“The sort order for each sort field MUST
be ascending unless it is prefixed with a
minus (U+002D HYPHEN-MINUS, “-“), in
which case it MUST be descending.”
Slide 53
Slide 53 text
No content
Slide 54
Slide 54 text
No content
Slide 55
Slide 55 text
No content
Slide 56
Slide 56 text
Use the JSON API `sort`
parameter without transform
Slide 57
Slide 57 text
Pagination
Slide 58
Slide 58 text
“A server MAY choose to limit the
number of resources returned in
a response to a subset (“page”)
of the whole set available.”
Slide 59
Slide 59 text
“Note: JSON API is agnostic about the
pagination strategy used by a server.
(…) The page query parameter can be
used as a basis for any of these
strategies.”
Slide 60
Slide 60 text
“Where specified, a meta member can be
used to include non-standard meta-
information. The value of each meta
member MUST be an object (a “meta
object”).”
Slide 61
Slide 61 text
No content
Slide 62
Slide 62 text
No content
Slide 63
Slide 63 text
No content
Slide 64
Slide 64 text
No content
Slide 65
Slide 65 text
Use JSON API’s `page`
parameters and pagination
meta data
Slide 66
Slide 66 text
Indicating ongoing
background record
fetching
Slide 67
Slide 67 text
No content
Slide 68
Slide 68 text
No content
Slide 69
Slide 69 text
No content
Slide 70
Slide 70 text
No content
Slide 71
Slide 71 text
No content
Slide 72
Slide 72 text
Eager return
Slide 73
Slide 73 text
No content
Slide 74
Slide 74 text
No content
Slide 75
Slide 75 text
Deconstruct the
background data fetching process
to have more control over the
loading process
Slide 76
Slide 76 text
Use
ember-concurrency
Slide 77
Slide 77 text
No content
Slide 78
Slide 78 text
No manual loading flag setting,
run loop scheduling and reload
option setting
Slide 79
Slide 79 text
https://balinterdi.com/
emberconf
Slide 80
Slide 80 text
No content
Slide 81
Slide 81 text
References
• Rock and Roll with Ember book
• {json:api} specification
• JSONAPI::Resources docs site
• ember-concurrency or How I Learned to Stop
Worrying and Love the Task on LinkedIn
Enginnering
• Lessons learned from four years with Ember by
Ryan Toronto