json-api-store icon indicating copy to clipboard operation
json-api-store copied to clipboard

Add a Store.meta() field

Open haydn opened this issue 9 years ago • 2 comments

var store = new Store();
store.define("products", {
  commentCount: Store.meta("total-comments"),
  comments: Store.hasMany()
});
store.define("comments", {});
store.add({
  type: "products",
  id: "1",
  meta: {
    "total-comments": 1
  },
  relationships: {
    comments: [
      { type: "comments", id: "1" }
    ]
  }
});

haydn avatar Oct 01 '15 02:10 haydn

test("load must return the meta property if it's included in the response", function (t) {
  var server = sinon.fakeServer.create({ autoRespond: false });
  var adapter = new Store.AjaxAdapter();
  var store = new Store(adapter);
  t.plan(1);
  t.timeoutAfter(1000);
  store.define("products", {});
  server.respondWith("GET", "/products?page[number]=5&page[size]=1", [
    200,
    { "Content-Type": "application/vnd.api+json" },
    JSON.stringify({
      meta: {
        totalItems: 4
      },
      data: [],
      included: []
    })
  ]);
  store.load("products", {
    page: {
      number: 5,
      size: 1
    }
  }).subscribe(function (products, meta) {
    t.equal(meta.totalItems, 4)
    t.pass("returns a successful response");
  }, function (error) {
    t.fail(error);
  });
  server.respond();
  server.restore();
});

This is a test I wrote, but when I started looking at the ajax-adapter, I had a hard time following code:

.do(() => store.add(data))
      .map(() => store.find(type, id))
      .publish();

Emerson avatar Mar 11 '16 21:03 Emerson

@Emerson Thanks for that! The code you're looking at there comes from RxJS. You can think of it like promises:

ajax(options)
  .then(() => store.add(data))
  .then(() => store.find(type, id));

I don't think you're going to be able to get that extra meta argument in there because the subscribe() method only passes a single argument to the onNext() handler:

https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/subscribe.md

Might have to tackle this another way. I'll let you know if I think of anything.

haydn avatar Mar 14 '16 00:03 haydn