Reflux REST mixin
✓ Published0🌍 Public
Aakre54
Last edited Jul 8, 2015
Created on Jul 8, 2015
This example demonstrates a Reflux mixin that syncs store actions with a REST API, exposing fetch, save, and delete operations that return promises. It uses the `window.fetch` API wrapped in a custom `sync` function, with `whatwg-fetch` polyfill and `defaults` for request headers. The mixin maps GET, POST, PUT, and DELETE requests to Reflux actions, triggering `.completed` or `.failed` handlers, and supports query-string serialization for GET params. Data flows through Reflux stores with `assign` for merging model updates and `JSON` for request/response bodies.
AI-generated descriptionCall actions on stores RESTfully:
// C
BotActions.save(null, {name: 'Curly', age: 26}); // POST a model without an id
// R
BotActions.fetch(); // GET the whole Collection
BotActions.fetch(2); // GET a single model by id
// U
BotActions.save(2, {name: 'Curly', age: 27}); // PUT a model by id
// D
BotActions.delete(2); // DELETE a model by id
All actions return promises:
BotActions.delete(2).then((id) => alert(Deleted ${id}))