dimanche 22 novembre 2015

Accessing the `links` property from a ember data query

When using the JSONAPIAdaper, and I query the store for a model and the server response include the "links" property specified by json-api spec, with a response like so.

{
  "links": {
    "self": "http://localhost:4200/api/v0/blog-posts?size=10",
    "first": "http://localhost:4200/api/v0/blog-posts?size=10&page=0",
    "last": "http://localhost:4200/api/v0/blog-posts?size=10&page=1",
    "next": "http://localhost:4200/api/v0/blog-posts?size=10&page=1"
  },
  "data": [{
    "id": 1,
    "type": "blog.posts",
    "attributes": {
      "published": "2015-04-04T00:56:36.768Z"
    },
    "relationships": {
      "revisions": {
        "data": [
          { "id": 1, "type": "blog.post.revisions" },
          { "id": 2, "type": "blog.post.revisions" },
          { "id": 3, "type": "blog.post.revisions" },
          { "id": 4, "type": "blog.post.revisions" }
        ]
      },
      "current": {
        "data": { "id": 4, "type": "blog.post.revisions" }
      }
    }
  }]
}

Note: I removed most of the elements in the data property and removed the included property as they make the example unnecessarily large.

The route that requests it looks like this

import Ember from 'ember';


export default Ember.Route.extend({
  model () {
    const store = this.get('store');
    return store.query('blog.post', { size: 10 });
  }
});

What I'm do is make a pagination mechanism for my blog by replacing the model with the data from the links specified in the links property.


Versions

  • ember @ 2.2
  • ember-data @ 2.2



Aucun commentaire:

Enregistrer un commentaire