Series: Introduction to Ember Data 2.0

Ember Data 2.0-Advanced Adapter Customizations

Published on Jan 22, 2016

In this video we dive deeper into Ember Data Adapter customization, going into customizations that are less common and more drastic than the ones we covered last time.

We first talk about the what goes into the core methods, using createRecord as our example. Then we look at the buildURL and ajax methods, finally seeing what happens when we overwrite urlForCreateRecord.


Links

Code

This episode shows how to make deeper customizations to your Ember Data Adapter.

There are several core methods you can directly overwrite:

  • createRecord
  • updateRecord
  • deleteRecord
  • findRecord
  • findAll
  • query
  • queryRecord

You can also directly overwrite the following relationship-focused methods:

  • findBelongsTo
  • findHasMany
  • findMany

What the above 10 methods have in common is that they all call buildURL and ajax (and, sometimes, the serializer).

buildURL is found in the build-url-mixin, and consists of sending off to a urlForX method based on the method that sent it in. The urlForX methods then send to the private method _buildURL which then creates the URL based on the arguments sent in by the urlForX method.

ajax is a private method which eventually calls jquery's ajax method. You shouldn't normally mess with it, but In the Adapter for this series we've hijacked it to help display the URLs instead of sending off to the server.

Transcript

In our last episode, we looked at the different URLs that are produced by the RESTAdapter, and we looked at common ways to change those, to customize it. In this episode, we’re going to be going even deeper. We’re going to be looking at some of the less common ways, more invasive ways, of customizing the adapter.

So the first and the most obvious is to write over something directly. So we can just completely write over the createRecord method. However that’s not typically recommended because there’s a lot that gets it done within that method.

So it’s different for each of the methods, but in createRecord, three main things happen. First, the URL is built up using various options. Then, the data is serialized into a hash. And then, you call out to the server using ajax.

So you can recreate all of this and create a new method called createRecord, but often times it’s easier to go deeper in, to change something within buildURL or something within ajax. You could also change stuff in the serializer, and we’ll get to serializeIntoHash later.

However, there is one case where it would actually be better to overwrite createRecord, and that’s when you need to change to HTTP verb, or if you need to change how this data is constructed, if you don’t want it all within data. But aside from that, we’ll want to go into buildURL or ajax to change stuff.

So buildURL is defined within the buildURLMixin, and the main method, it just takes the requestType and it calls out to several other methods. That’s what all the urlFor methods do. And then these urlFor methods are all defined down here. However, you can overwrite them individually.

Let’s go ahead and overwrite urlForCreateRecord. And so that’ll take the modelName and the snapshot, and then we could do a lot of processing. We’ll just return a string for now.

And one thing you’ll notice is that the host and the namespace are not applied. It’s not tacodeli.com/api/v1/hello. It’s just hello. And that is because urlForCreateRecord calls buildURL, the private version, and that private version is where all the magic happens. So this is where the urlPrefix and the host are added on and where a lot of other processing occurs.

That’s not to say you shouldn’t overwrite urlForCreateRecord. It’s just that you should know what you’re replacing, the complexity of it, before you go and replace it so you can make up for some of that complexity.

So that’s a buildURL. Then we have the serializer which we’re calling serializeIntoHash on, and we’ll go over that when we go over the serializers. And then we call ajax. Ajax is where we prepare and then call the ajax call. So it’s a promise and we’ll define something for what will happen when it’s successful, and when it fails, and then we go ahead and call it. You can of course overwrite ajax, and that’s what I’ve done in order to create this app, right here.

So here instead of calling out to a server, I mock up some stuff based on what Ember Data expects back, and then I just resolve it while sending out to my urlTracker so that I can display it on the screen.

While we’re here, you’ll also notice that I overrode buildURL as well, but in this case, I have it returning super, and so it ends up calling buildURL as it was before in addition to what I did.

So in this episode, we went over some methods that are pretty core to what the RESTAdapter does but aren’t necessarily overridden as much as the ones we went over last time. Those are buildURL, the urlForX methods, as well as the ones that are directly called like createRecord or deleteRecord. We also went over the ajax method which you’ll notice isn’t here. That’s because it’s a private method. So ajax is really not meant to be overridden, although I did it because I had a very special use case.

So those are all the core ones but you’ll notice that there are still some that we haven’t gone over. We’ll go over those miscellaneous methods in the next episode. I’ll see you then.

Introduction to Ember Data 2.0

Subscribe to our mailing list