Models

AJAX Abstraction for a Single Resource

retrieve

This page describes how to retrieve a resource using a Model.

Basic Usage

Let's say you have an API endpoint located at http://localhost:1337/tweets and that there is a tweet you want to retrieve that looks like this:

{
  id: 1,
  text: 'An old tweet'
}

To retrieve that Tweet, we need to issue a GET request to http://localhost:1337/tweets/1 that looks like this:

GET http://localhost:1337/tweets/1

The code below demonstrates how to do that using a Model:

import { Model } from 'lore-models';

const Tweet = Model.extend({
  urlRoot: 'http://localhost:1337/tweets'
});

const tweet = new Tweet({
  id: 1
})

tweet.fetch()

First we import Model from lore-models. Then we create a new model called Tweet, and provide the location of the API endpoint we want to interact with as the urlRoot property.

Once we have the Tweet model created, we create an instance of that model, and provide the id of the resource we want to retrieve. Then we call tweet.fetch() to retrieve that resource through the API.

The tweet.fetch() will be transformed into a GET request, and an AJAX request will be sent that looks like this:

GET http://localhost:1337/tweets/1

Once the AJAX request returns, the tweet instance will be automatically updated, and you'll be able to view the resource's attributes via the attributes field of the instance, like this:

model.attributes = {
  id: 1,
  text: 'An old tweet'
}

The tweet.fetch() method returns a promise. If you need access to the response object once the API call returns, you can do that using this code:

tweet.fetch().then(function(response) {
  // do something with response
})

The response object will have a structure that looks like this:

const response = {
  config: {
    headers: {
      Accept: "application/json, text/plain, */*"
    },
    method: "GET",
    responseType: "json",
    url: "http://localhost:1337/tweets/1"
  },
  data: {
    id: 1,
    title: "Some Text",
    createdAt: "2016-07-02T00:00:06.407Z",
    updatedAt: "2016-07-02T00:00:06.407Z"
  },
  headers: {
    "content-type": "application/json"
  },
  request: {...}, // XMLHttpRequest
  status: 200,
  statusText: "OK"
};

Advanced Usage

Models use the axios library for handling for all network request, and that means the properties you set on models are ultimately just converted into a config object that axios understands.

For the fetch() method, that means the tweet.fetch() call we demonstrated above is ultimately converted into this axios call:

import axios from 'axios';

axios({
  url: 'http://localhost:1337/tweets/1',
  method: 'get',
  headers: {
    'Content-Type': 'application/json'
  }
})

If you need more control over your network requests, such as adding headers, simply provide an options object to get tweet.fetch() call like this:

tweet.fetch({
  headers: {
    'Content-Type': 'application/json'
    Authorization: 'Bearer token'
  }
})

This object will be passed directly to the axios call.

You can learn about all of the config options that axiossupports here.