Latest / CyberSecurity Summary / Pro Ember Data: Getting Ember Data to Work with Your API
Transcript
- 0:00Speaker 1: Welcome back to the deep died curious minds. You know
- 0:02Speaker 1: that feeling you've got this really powerful tool, maybe like
- 0:05Speaker 1: a brand new engine, but your current setup, your infrastructure,
- 0:09Speaker 1: maybe uses a slightly different kind of fuel. It doesn't
- 0:11Speaker 1: quite mesh. Well, today we're tackling exactly that kind of challenge.
- 0:15Speaker 1: But in the world of web development, specifically with mber Data.
- 0:18Speaker 1: It's the data layer for mber dotjs apps and well,
- 0:22Speaker 1: it's designed to make interacting with APIs and managing your
- 0:25Speaker 1: data really smooth, supercharge that user experience, you know. But
- 0:29Speaker 1: here's the kicker. What if your API isn't shall we
- 0:32Speaker 1: say standard, What if it uses different naming or weird
- 0:36Speaker 1: URL structures or throws errors you don't expect. That's what
- 0:39Speaker 1: we're digging into today. How mber data is a really
- 0:42Speaker 1: smart design actually lets you adapt to any API out there.
- 0:45Speaker 1: Think of it like getting a universal translator. Yeah, making
- 0:48Speaker 1: your app and your back end speak the same language,
- 0:50Speaker 1: no matter how quirky that back end might be.
- 0:52Speaker 2: Exactly that's a challenge a lot of developers face, right,
- 0:55Speaker 2: integrating a really robust front end like ember data with
- 0:59Speaker 2: an excus sting back end API that might be well,
- 1:03Speaker 2: let's call it unique. Ember Data definitely has its own
- 1:06Speaker 2: opinionated way of doing things, it's preferred conventions. But its
- 1:10Speaker 2: real power, I think, is in how extensible it is.
- 1:13Speaker 2: We're going to look at its core parts, the store,
- 1:15Speaker 2: the adapters, and the serializers and see how understanding those
- 1:19Speaker 2: lets you connect to pretty much any data source. It
- 1:22Speaker 2: really saves you from reinventing the wheel for common stuff
- 1:25Speaker 2: like identity mapping or managing relationships.
- 1:27Speaker 3: Okay, let's unpack that. An analogy might help imagine. Your app.
- 1:30Speaker 3: Is this like bustling city and all your data users
- 1:33Speaker 3: products whatever they're the citizens moving around. Mber Data is
- 1:37Speaker 3: like the city management system, keeping track of everything, and
- 1:40Speaker 3: at its heart there are these three key departments running
- 1:42Speaker 3: the show. First up, you mentioned the store. What's that
- 1:45Speaker 3: like city hall?
- 1:46Speaker 2: Sort of? Yeah, think of the store as the city's
- 1:48Speaker 2: central records office, but also like a super fast memory bank.
- 1:51Speaker 2: When your application needs some data, it doesn't go straight
- 1:54Speaker 2: to the API. It asks the store first.
- 1:57Speaker 3: Ah, so it checks its cap precisely, and when data
- 1:59Speaker 3: comes in, the store caches it intelligently. But it's more
- 2:03Speaker 3: than just a simple cash The really clever part is
- 2:06Speaker 3: the identity map. This prevents you from having say two
- 2:09Speaker 3: different JavaScript objects representing the exact same user record and memory.
- 2:13Speaker 3: How does that work? Well, imagine you fetch a list
- 2:16Speaker 3: of contacts, maybe GD contacts, and later you fetch a
- 2:19Speaker 3: specific one GD contacts one. If contact one was in
- 2:24Speaker 3: both responses, the store ensures you only ever have one
- 2:27Speaker 3: object instance for that contact with EED one. It preserves
- 2:31Speaker 3: that object identity.
- 2:33Speaker 2: Okay, I see, so no confusing duplicates floating around. That
- 2:36Speaker 2: sounds vital for keeping things consistent, especially in complex apps.
- 2:39Speaker 3: Absolutely crucial for scalability and uh just general sanity. Really.
- 2:44Speaker 2: So, the store holds the records, keeps them unique. But
- 2:47Speaker 2: how does it actually, you know, talk to the outside world,
- 2:50Speaker 2: to our potentially quirky API.
- 2:52Speaker 3: Right, The store doesn't talk directly. It delegates. That's the
- 2:55Speaker 3: job of the adapter.
- 2:56Speaker 2: The adapter. Okay, so if the store City Hall, the
- 2:58Speaker 2: adapter is the Department of Foreign Affairs. Huh yeah, that's
- 3:02Speaker 2: a good way to put it. The adapter handles how
- 3:04Speaker 2: to communicate with external data sources your API. Its job
- 3:08Speaker 2: is to figure out the full URL for a request,
- 3:10Speaker 2: actually make the HTTP call using ajax, fetch maybe even
- 3:15Speaker 2: web sockets, and it can even talk to things that
- 3:18Speaker 2: aren't remote APIs like the browser's local storage or index
- 3:21Speaker 2: dB possible very and the key thing is this separation.
- 3:25Speaker 2: If your API changes how it expects requests, maybe moves
- 3:29Speaker 2: from rest to grah ql, or just changes its URL scheme,
- 3:32Speaker 2: often you only need to update the adapter, not rewrite
- 3:36Speaker 2: huge chunks of your application logic decoupling nice exactly. Ember
- 3:40Speaker 2: Data ships with a few standard ones REST adapter for
- 3:43Speaker 2: common rest stuff JSON APA adapter, which is the default
- 3:46Speaker 2: and follows the Jason Dot API spec and there used
- 3:48Speaker 2: to be an active model adapter for rails though that's
- 3:51Speaker 2: an add on now.
- 3:51Speaker 3: Okay, So the adaptor handles the where and how of
- 3:54Speaker 3: the request the transport layer basically, But what about the
- 3:58Speaker 3: actual data the payload? What if the app I sends
- 4:00Speaker 3: back data that looks completely different from what mber data expects,
- 4:03Speaker 3: different grammar, different vocabulary.
- 4:05Speaker 2: That, my friend, is where the serializer steps in.
- 4:07Speaker 3: The serializer the linguist.
- 4:09Speaker 2: Precisely, The serializer is your data's translator. It has two
- 4:13Speaker 2: main jobs. First, serialization, taking data from your Mber app
- 4:18Speaker 2: and formatting it correctly to send to the server. Second, normalization,
- 4:24Speaker 2: taking the raw data received from the server and translating
- 4:27Speaker 2: it into a structure. Mber Data understands, so.
- 4:30Speaker 3: Adapter handles the connection. Serializer handles the language you got it.
- 4:34Speaker 2: That separation is fundamental to ember Data's adaptability. It lets
- 4:38Speaker 2: you tackle the how to talk and the what to
- 4:40Speaker 2: say independently and.
- 4:41Speaker 3: I'm guessing just like adapters, there are different serializers for
- 4:45Speaker 3: different API languages.
- 4:46Speaker 2: Spot on, mber Data gives you three main ones out
- 4:48Speaker 2: of the box. There's the basic Jason serializer for simple
- 4:52Speaker 2: flat Jason good starting points sometimes, and the rest serializer.
- 4:56Speaker 2: This one expects data wrapped in a root key like contact,
- 4:59Speaker 2: and it also understands how to handle sideloading.
- 5:01Speaker 3: Sideloading what's that?
- 5:02Speaker 2: Oh, that's when an API includes related data in the
- 5:06Speaker 2: same response to save extra network requests like getting a
- 5:09Speaker 2: contact and their company details all at once. The rest
- 5:12Speaker 2: serializer can unpack.
- 5:13Speaker 3: That gotcha, reduces chattiness exactly.
- 5:16Speaker 2: And finally, there's the Jason up a serializer, which is
- 5:19Speaker 2: built specifically for APIs that strictly follow the official Jason
- 5:24Speaker 2: dot API specification. That's often the default. The idea is
- 5:28Speaker 2: you pick the built in serializer that's closest to what
- 5:30Speaker 2: your API does, and that minimizes the custom transation work
- 5:33Speaker 2: you need to do initially.
- 5:35Speaker 3: Okay, this is where it gets really practical for you listening,
- 5:38Speaker 3: because let's face it, real world APIs aren't always textbook
- 5:41Speaker 3: perfect are they? Rarely ember data anticipates this. That's why
- 5:45Speaker 3: these customization hooks exist. It's about making your app talk
- 5:48Speaker 3: smoothly to your specific back end warts and all. Let's
- 5:51Speaker 3: start with customizing adapters. Say my API uses singular nouns
- 5:56Speaker 3: for URLs like contact one instead of contacts one. How
- 5:59Speaker 3: do I tell data?
- 6:00Speaker 2: Yeah, that's a common one. It's surprisingly easy. You just
- 6:02Speaker 2: override a method called pathsfotype in the adapter for that
- 6:05Speaker 2: specific model. So in your app adapters contact dot js file,
- 6:08Speaker 2: you'd add a path four type model name function and
- 6:11Speaker 2: just make it return the singular model name. Done. Simple enough,
- 6:15Speaker 2: so that handles the base path. But what if I
- 6:17Speaker 2: need a totally custom URL structure for a specific query,
- 6:21Speaker 2: like searching contacts by city, but the API wants app
- 6:24Speaker 2: contacts by city Los Angeles instead of a query peram
- 6:27Speaker 2: like dot city los Angeles.
- 6:28Speaker 3: Right, for specific query logic, you'd override are all four query. Again,
- 6:32Speaker 3: probably in your contact adapter. That method gets the queer
- 6:35Speaker 3: parameters and the model name, so you have everything you
- 6:37Speaker 3: need to build that custom URL string like app contacts
- 6:41Speaker 3: by cityquery dot city and yeah, you might need to
- 6:44Speaker 3: remember to dasherize the city name turn los Angeles into
- 6:47Speaker 3: los Angeles, which is pretty standard for URLs.
- 6:49Speaker 2: Okay, that makes sense. You get full control over the
- 6:52Speaker 2: URL generation for different actions. And thinking about, say using
- 6:55Speaker 2: an external API like reddits. Their API for subreddit posts
- 6:58Speaker 2: is something like reddit dot com. Rememberjs dot Jason, we'd
- 7:02Speaker 2: use exactly that role for query override in a post
- 7:04Speaker 2: adapter to hit that specific nonstandard URL.
- 7:07Speaker 4: Right.
- 7:07Speaker 2: Absolutely. That's a perfect example of adapting to a completely external,
- 7:11Speaker 2: maybe slightly unusual API structure using the adapter hooks. And
- 7:16Speaker 2: while we're talking adapters, there's more fine grain control too.
- 7:19Speaker 2: You can influence how ember data decides whether to refetch
- 7:22Speaker 2: data that's already in the store, methods like should background
- 7:25Speaker 2: reload all or should background reload record. Let you return
- 7:28Speaker 2: true or false to control background updates.
- 7:31Speaker 3: So you can fine tune the caching behavior prevent unnecessary requests,
- 7:35Speaker 3: but still keep data fresh.
- 7:37Speaker 2: Precisely, maybe for certain data you always want the latest,
- 7:40Speaker 2: so you force a reload for other less critical data.
- 7:43Speaker 2: The cased version is fine for longer.
- 7:45Speaker 3: Very cool. Okay, let's switch gears to serializers. The translators.
- 7:49Speaker 3: APIs love different naming conventions, don't they snake case like
- 7:53Speaker 3: first name, title case CamelCase. Our JavaScript usually prefers CamelCase.
- 7:58Speaker 3: How do we bridge that gap without making our model ugly?
- 8:00Speaker 2: Oh yeah, the casing battle. Mber data has elegant solutions
- 8:04Speaker 2: for this. For just one or two specific fields, you
- 8:06Speaker 2: can use the actress hash in your serializer. You map
- 8:09Speaker 2: the model's camel case name to the API's key like
- 8:12Speaker 2: first name.
- 8:13Speaker 3: Oh, first name okay for isolated cases.
- 8:15Speaker 2: But if your entile API uses, say, snake case, doing
- 8:19Speaker 2: that for every attribute would be tedious. So instead you
- 8:21Speaker 2: override a method called key for attribute in your main
- 8:24Speaker 2: application serializer. This function receives the camel case attribute name
- 8:28Speaker 2: from your model, and you just return the transformed snakecase version.
- 8:32Speaker 2: Ember data then uses this for all attributes.
- 8:35Speaker 3: Automatically, nice like setting a global.
- 8:38Speaker 2: Rule exactly and again. Back to that Reddit example. Reddit
- 8:41Speaker 2: uses snake case for things like numb comments. Right, So
- 8:45Speaker 2: overwriting key for attribute lets our mber model use a
- 8:48Speaker 2: clean numb comments while the serializer handles translating it to
- 8:51Speaker 2: numb comments when talking to the Reddit API keeps the
- 8:54Speaker 2: front end cord nice and idiomatic.
- 8:56Speaker 3: Love it. What about relationships? APIs often just give you
- 8:59Speaker 3: a foreign key like companied. How do we link that
- 9:02Speaker 3: to a proper belongs to company relationship?
- 9:05Speaker 2: In Ember another common pattern, you can override key for
- 9:09Speaker 2: relationship in your serializer, similar to key for attribute. This
- 9:13Speaker 2: method lets you tell ember Data how relationship keys are
- 9:17Speaker 2: represented in the payload. You could tell it to expect
- 9:20Speaker 2: ad appended to the relationship name. For instance, so company
- 9:24Speaker 2: becomes companied, so.
- 9:25Speaker 3: It automates linking company to the company relationship. Yeap.
- 9:28Speaker 2: And one more quick one, what if your API doesn't
- 9:31Speaker 2: use e'd as the primary key, maybe uses eed or
- 9:34Speaker 2: SSN or something else. Just set the primary key property
- 9:37Speaker 2: on your serializer to whatever the API uses like primary
- 9:40Speaker 2: key ad. Mber Data will then know how to uniquely
- 9:43Speaker 2: identify your records.
- 9:45Speaker 3: Okay, handling keys casing relationships. That covers a lot of
- 9:48Speaker 3: common API quirks. Yeah, but what if the whole structure
- 9:52Speaker 3: of the response is just different, not just the keys,
- 9:55Speaker 3: but the nesting, the wrapping, Like that Reddit example where
- 9:57Speaker 3: the good stuff is buried deep.
- 9:59Speaker 2: Now we're talking normalization. This is the serializer's superpower for
- 10:03Speaker 2: reshaping incoming data. Let's say your API wraps everything in
- 10:07Speaker 2: a generic data key like data Yeah, you're chosen. Serializer
- 10:10Speaker 2: might not expect that wrapper, yes, so you override normalizer response.
- 10:15Speaker 2: This method gets the raw payload straight from the adapter.
- 10:19Speaker 2: Inside normalizer response, you can manipulate that payload. You could
- 10:23Speaker 2: say payload equal payload dot data to unwrap it. Then
- 10:27Speaker 2: crucially you call super dot normalizer response store primary model class, payload,
- 10:32Speaker 2: ID request type, passing the modified payload along. This lets
- 10:36Speaker 2: the standard normalization process take over with data it now understands.
- 10:40Speaker 3: Is there like preprocessing the raw response before Mberdata's main
- 10:44Speaker 3: normalization engine.
- 10:45Speaker 2: Sees it exactly, You're cleaning it up, reshaping it into
- 10:48Speaker 2: the format your base serializer, like Jason serializer Arrest serializer expects.
- 10:52Speaker 2: Remember ember Data's internal format it aims for is kind
- 10:55Speaker 2: of like jason dot API, but with singular, dasherized types
- 10:58Speaker 2: and camelcased attributes. Normalization bridges the gap between your API
- 11:02Speaker 2: and that internal format.
- 11:03Speaker 3: And for that Reddit example, with data nested under data
- 11:05Speaker 3: dot children, not data, that's where normalized response or maybe
- 11:09Speaker 3: a more specific one like normalized array response.
- 11:11Speaker 2: Comes in precisely, you'd override the appropriate normalized method, dig
- 11:16Speaker 2: into that nested structure, extract the array of actual post data,
- 11:20Speaker 2: maybe do some transformation on each item, and then pass
- 11:23Speaker 2: that cleaned up array to super makes sense.
- 11:26Speaker 3: It lets you handle really arbitrarily structured APIs.
- 11:29Speaker 2: It does, And normalized response actually delegates to more specific
- 11:32Speaker 2: methods based on the type of request normalized final response,
- 11:36Speaker 2: normalized find record response, normalized save response, etc. So you
- 11:39Speaker 2: can target your normalization logic very precisely if needed.
- 11:43Speaker 3: Super granular control. Now, when we send data back saving
- 11:47Speaker 3: or updating, you mentioned something called a snapshot object. Why
- 11:50Speaker 3: is that important?
- 11:51Speaker 2: Ah, yes, the snapshot. When you save a record, the
- 11:54Speaker 2: adapter doesn't get the live ember data record directly, it
- 11:57Speaker 2: gets a snapshot. Think of the snapshot as a re
- 12:00Speaker 2: only sort of frozen representation of the record's data at
- 12:03Speaker 2: that specific moment in time.
- 12:04Speaker 3: Okay, why not just use the live record.
- 12:06Speaker 2: Because accessing properties on a live Ember data record can
- 12:09Speaker 2: sometimes trigger side effects like automatically fetching unloaded relationships. You
- 12:14Speaker 2: don't want that happening accidentally when you're just trying to
- 12:16Speaker 2: package up data to send to the server. The snapshot
- 12:19Speaker 2: provides a safe, stable copy of the data for the
- 12:22Speaker 2: serializer to work with. Serialize into hash serialized methods. Use
- 12:25Speaker 2: it without any unexpected side effects. It guarantees you're only
- 12:29Speaker 2: sending the intended data.
- 12:30Speaker 3: Got it. It's like a safe copy for serialization purposes,
- 12:34Speaker 3: prevents unexpected data.
- 12:35Speaker 2: Loading exactly keeps things predictable.
- 12:38Speaker 3: Wow. Okay, that covers a ton of ground on adapting
- 12:41Speaker 3: the core communication. But what about more complex data patterns
- 12:47Speaker 3: like nested objects. What if my API sends back an
- 12:50Speaker 3: address object right inside the contact payload?
- 12:53Speaker 2: Good question. Mber data gives you a couple of options there.
- 12:56Speaker 2: For simple read only nested objects, where the address doesn't
- 12:58Speaker 2: really need to be its own manager, you can just
- 13:01Speaker 2: declare the attribute without a specific type or transform like
- 13:04Speaker 2: it at Bautry address. Mber data will just treat it
- 13:07Speaker 2: as a plain JavaScript object and pass it through Simple.
- 13:10Speaker 3: Okay for basic nested blobs of data.
- 13:13Speaker 2: But if that nested object like the address, has its
- 13:16Speaker 2: own ID and conceptually is a separate entity, you might
- 13:19Speaker 2: want to manage your link to elsewhere. Then you can
- 13:22Speaker 2: treat it as an embedded record. You use the embedded
- 13:25Speaker 2: records mixin in your serializer. This tells the serializer to
- 13:29Speaker 2: look for those nested objects and actually turn them into proper,
- 13:32Speaker 2: fully fledged MBER data records, establishing the has many or
- 13:36Speaker 2: belongs to relationship automatically, so.
- 13:38Speaker 3: It extracts the nested AID one three street mood yeah
- 13:41Speaker 3: and makes it a real address record linked.
- 13:43Speaker 2: To the contact precisely. Like if you have skills AID
- 13:47Speaker 2: one name, MBRA eighty two name texting embedded in a
- 13:51Speaker 2: user payload, the mixin can turn those into actual skill
- 13:54Speaker 2: records related to the user. One important caveat, though the
- 13:57Speaker 2: embedded records mixin is designed primarily for the rest serializer
- 14:01Speaker 2: or custom serializers, it doesn't really play nicely with the
- 14:03Speaker 2: strict Jason a PI serializer.
- 14:05Speaker 3: Okay, so if you're using strict Jason, that API embedding
- 14:08Speaker 3: isn't the standard approach anyway, Right, you'd use relationship.
- 14:11Speaker 2: Links exactly JSON that API prefers relationships defined by type
- 14:16Speaker 2: addresses ED one twenty three in a relationships block rather
- 14:20Speaker 2: than embedding the full address object directly under the contact.
- 14:24Speaker 3: That makes sense. Use the tool that fits the standard
- 14:26Speaker 3: you're working with.
- 14:26Speaker 2: Right, Embedded records are powerful for certain API styles, but
- 14:29Speaker 2: maybe not the JSON dot API way.
- 14:32Speaker 3: Okay, moving on errors they happen. How does mber data
- 14:36Speaker 3: help us handle API errors gracefully?
- 14:38Speaker 2: It actually has pretty robust built in error handling. It
- 14:41Speaker 2: maps common HTTP status codes to specific error objects. The
- 14:45Speaker 2: most common one is probably four hundred and twenty two
- 14:47Speaker 2: unprocessable entity. Mber data treats this as a validation error
- 14:51Speaker 2: and creates an invalid air object. Importantly, this invalid air
- 14:55Speaker 2: expects the API response to follow the JSON dot API
- 14:58Speaker 2: error object specification with d DE tales about what went wrong,
- 15:01Speaker 2: often including pointers to the specific fields.
- 15:03Speaker 3: So it expects errors detail marted source pointer data attributes.
- 15:08Speaker 2: Femail ideally yes. That allows you to easily display validation
- 15:12Speaker 2: messages next to the correct form fields. For example, It
- 15:15Speaker 2: also maps other codes four one becomes an unauthorized error,
- 15:19Speaker 2: four or three of forbidden AER for a four and
- 15:21Speaker 2: not founder four on nine a conflict error, and five
- 15:24Speaker 2: hundred range usually a server er. These all inherit from
- 15:27Speaker 2: a base adapter AIR class.
- 15:28Speaker 3: That's handy, but what if my API uses say, four
- 15:32Speaker 3: hundred bad requests for validation airs instead of four to
- 15:34Speaker 3: twenty two.
- 15:35Speaker 2: You can customize that the adapter has an is invalid
- 15:38Speaker 2: status Header's payload method, you can override it to return true.
- 15:43Speaker 2: If the status code is four hundred or whatever your
- 15:45Speaker 2: API uses telling ember data to treat it as an
- 15:48Speaker 2: invalid error.
- 15:49Speaker 3: Okay, so we can map status codes. What about the
- 15:51Speaker 3: error payload itself? What if it's not Jason dot API compliant.
- 15:54Speaker 2: Two main places to hook in there. For general error
- 15:57Speaker 2: response shaping, you can override handle response in adapter that
- 16:00Speaker 2: gives you the raw response regardless of status. But if
- 16:03Speaker 2: you specifically want to normalize the structure of validation error messages,
- 16:07Speaker 2: like transforming your API's custom air format into the json
- 16:10Speaker 2: dot api ers array, the extra errors method on the
- 16:13Speaker 2: serializer is usually the better place. It's semantically focused on error.
- 16:16Speaker 3: Payload interpretation adapter for status code mapping and general handling
- 16:21Speaker 3: serializer for normalizing the air content.
- 16:24Speaker 2: Got you got it.
- 16:24Speaker 3: Let's tackle something a bit more mind vending. Polymorphic relationships,
- 16:29Speaker 3: where a relationship can point to different types of records,
- 16:32Speaker 3: like a notification could be about a new comment or
- 16:34Speaker 3: a friend request. Ah.
- 16:36Speaker 2: Yes, polymorphism very powerful for certain data models. You enable
- 16:40Speaker 2: this in your model by setting polymorphic true on the
- 16:43Speaker 2: belongs to or has many relationship definition. The key requirement
- 16:47Speaker 2: then falls on the API response When including that relationship
- 16:51Speaker 2: data either sideloaded or linked. The API must provide both
- 16:54Speaker 2: the ID and the type for each related record.
- 16:57Speaker 3: So like add five foot type new comment or ABC
- 17:01Speaker 3: type friend request.
- 17:02Speaker 2: Exactly that type tells Ember data which specific model new
- 17:06Speaker 2: comment or friend request to load for that relationship link.
- 17:09Speaker 2: Of course, those models should ideally inherit from a common
- 17:12Speaker 2: base model like notification subject and can.
- 17:14Speaker 3: You customize how that type information is read or written
- 17:18Speaker 3: if the EPI uses a non standard way.
- 17:20Speaker 2: Absolutely. The serializer offers hooks like key for polymorphic type,
- 17:24Speaker 2: serialize polymorphic type, and extract polymorphic relationship to handle custom
- 17:29Speaker 2: conventions for identifying and processing those polymorphic types in the payload.
- 17:34Speaker 3: Incredible flexibility there. It really feels like you can mold
- 17:37Speaker 3: MBER data to almost anything, which brings us to testing.
- 17:40Speaker 3: With all these potential customizations and adapters and serializers, how
- 17:44Speaker 3: do we make sure they actually work correctly and don't
- 17:46Speaker 3: break later?
- 17:47Speaker 2: Testing is absolutely critical. Yes, you wouldn't build a complex
- 17:50Speaker 2: machine without testing the parts right. You can unit test
- 17:53Speaker 2: adapters and serializers in isolation. For an adapter, you might
- 17:57Speaker 2: call EARL four query directly and assert the output URL.
- 18:01Speaker 2: For a serializer, you could test key for attribute or
- 18:04Speaker 2: normalize with sample payloads.
- 18:05Speaker 3: Okay, unit tests for specific methods.
- 18:07Speaker 2: But often the most valuable tests are integration tests that
- 18:10Speaker 2: exercise the adapter and serializer together working through the store.
- 18:14Speaker 2: This more closely mirrors how your application actually uses them.
- 18:18Speaker 2: You'd use the store to find or save a record,
- 18:20Speaker 2: and then you can assert things about the outgoing request,
- 18:23Speaker 2: like the URL or payload sent by the adapter, serial
- 18:26Speaker 2: ether combo, or the data that ends up in the
- 18:29Speaker 2: store after normalization.
- 18:31Speaker 3: And how do you do that without hitting a real API.
- 18:33Speaker 2: Tools like Embercli Mirage are perfect for this. Mirage lets
- 18:37Speaker 2: you easily define mock API endpoints right within your Mber app.
- 18:41Speaker 2: So your test run against this predictable mock server. You
- 18:44Speaker 2: can make the store fetch data, Mirage intercepts it returns
- 18:47Speaker 2: a specific payload you defined, and then you can assert
- 18:50Speaker 2: that your serializer normalized it correctly into the store, or
- 18:53Speaker 2: assert that saving a record sent the correctly serialized payload
- 18:57Speaker 2: to Mirage.
- 18:58Speaker 3: So Mirage acts as the customized fake back end for
- 19:01Speaker 3: testing the whole store adapter serializer stack precisely.
- 19:04Speaker 2: It gives you high confidence that your data layer customizations
- 19:06Speaker 2: are working as intended.
- 19:08Speaker 3: Right, That makes a lot of sense. Test the whole flow.
- 19:10Speaker 3: So let's tie this all together. What's the big takeaway
- 19:12Speaker 3: for everyone listening. We've gone through the core part store, adapter, serializer.
- 19:17Speaker 3: We've seen how to tweak URLs, map keys, handle nesting,
- 19:21Speaker 3: manage errors, even deal with polymorphism.
- 19:24Speaker 2: I think the main point is that ember data isn't
- 19:27Speaker 2: just about fetching data in one specific, prescribed way. It's
- 19:30Speaker 2: really about giving you, the developer, the power to create
- 19:34Speaker 2: clean abstractions over any kind of API, no matter how
- 19:38Speaker 2: complex or non standard it might be. It provides strong
- 19:41Speaker 2: conventions as a starting point, which is great for productivity,
- 19:44Speaker 2: but it's real strength, it's genius lies in these thoughtful
- 19:48Speaker 2: extension points, the adapter and serializer hooks that let you
- 19:51Speaker 2: handle the inevitable real world variations. It frees you up
- 19:55Speaker 2: to focus on building the actual features and user experience,
- 19:57Speaker 2: rather than getting bogged down in the messy detail of
- 20:00Speaker 2: data fetching and translation for every single API call.
- 20:03Speaker 3: So, whether your API is a perfect JSON dot API
- 20:06Speaker 3: implementation or something a bit more creative. From the back
- 20:11Speaker 3: end team.
- 20:11Speaker 4: Mber Data's extensibility means you likely have the tools to
- 20:14Speaker 4: make it work cleanly within your MBER application. Hopefully this
- 20:17Speaker 4: deep dive has shown you not just how to use
- 20:18Speaker 4: these tools, but why they're designed the way they are,
- 20:21Speaker 4: empowering you to really master your application's data flow. Think
- 20:24Speaker 4: about your own projects. Which of these techniques Overwriting EARL
- 20:27Speaker 4: for query, customizing key for attribute, using normalized response might
- 20:31Speaker 4: solve that nagging API integration headache you've been avoiding. The
- 20:34Speaker 4: tools are there. That's our time for this deep dive.
- 20:37Speaker 4: Until next time, keep exploring, keep customizing, and keep learning.