Modify how findRecord works, customize response parsing

Hi, I’m trying to modify how findRecord works. I’m using the RESTAdapter, and I have something like this:

this.store.findRecord('house', 1);

which generates a URL like /v1/houses/1?timestamp=12345 . I’ve added the query params functionality for findRecord, by modifying the REST adapter. But that’s not related to my question.

The API response looks something like this:

{
    "house": {
          "id": 1,
          "street": "name",
          "number": 20,
          "timestamp": 12345
      }
}

Let’s imagine that generating this data is a complicated and relatively slow task for the backend. So if the backend detects that the timestamp sent as a query param is up to date, comparing it to the one in the database, it could simply respond to the request with:

{
    "up_to_date": true
}

My question is how can I make findRecord accept this type of API response. It should be something like this:

  • when trying to load the house with id 1 the first time, the app won’t have any timestamp and it will have no model in the store, so it calls the backend which responds with a full house response, and a timestamp.
  • when trying to load it the second time, the app should call findRecord and send the timestamp. If the server responds with up_to_date, the app should use the model which is already saved in the store

Right now findRecord complains about not finding the model key in the response, and the error action is triggered. But I want it to respond either with the model in the store, if the API json contains up_to_date, or it could somehow let me know that it is up to date, inside the model hook of the route, so I can call peekRecordthere and return that as a model.

But I could not find a way to modify the behaviour of findRecord. If anybody has any idea how to do this, I would appreciate it so so much. findRecord is called in inside the model hook of a route.

Hi, so I fixed my problem by overriding handleResponse in the adapter, and returning a custom UpToDateError, which I am able to catch in the route model hook, and load the store record, using peekRecord.

1 Like