acdlite/flummox

View on GitHub
docs/dist/flummox/data/docs/api/actions.json

Summary

Maintainability
Test Coverage
{"path":"api/actions","content":"`Actions`\n=========\n\nCreate actions by extending from the base `Actions` class.\n\n```js\nclass MessageActions extends Actions {\n\n  // Methods on the prototype are automatically converted into actions\n  newMessage(content) {\n\n    // The return value from the action is sent to the dispatcher.\n    // It is also returned to the caller.\n    return content;\n  }\n\n  // Asynchronous functions are also supported: just return a promise\n  // This is easy using async-await\n  async createMessage(messageContent) {\n    const response = await serverCreateMessage(messageContent);\n    return await response.json();\n  }\n\n}\n```\n\nYou can also use a plain JavaScript object. When passed to `flux.createActions`, it will be converted into an Actions class.\n\n```js\n// Same as previous example\nconst MessageActions = {\n  newMessage(content) {\n    return content;\n  },\n\n  async createMessage(messageContent) {\n    const response = await serverCreateMessage(messageContent);\n    return await response.json();\n  }\n}\n```\n\nTesting\n-------\n\nThe return value of an action is dispatched automatically. It's also returned to the caller. This means it's possible to test actions completely independently from a Flux or Store instance. Here's how you'd test the example MessageActions from above:\n\n```js\n// Using mocha and chai-as-promised\nconst actions = new MessageActions();\n\nexpect(actions.newMessage('Hello world!')).to.equal('Hello world');\n\n// Assuming `serverCreateMessage()` has been mocked\nexpect(actions.createMessage('Hello world!')).to.eventually.deep.equal({\n  id: 1,\n  content: 'Hello world!',\n});\n```\n\n\nAsynchronous actions\n--------------------\n\nAsynchronous actions are actions that return promises. Unlike synchronous actions, async actions fire the dispatcher twice: at the beginning and at the end of the action. Refer to the [Store API](store.md) for information on how to register handlers for asynchronous actions.\n\nMethods\n-------\n\n### getActionIds\n\n```js\nobject getActionIds()\n```\n\nReturns an object of action ids, keyed by action name. (In most cases, it's probably more convenient to use `Flux#getActionIds()` instead.)\n\n\nAlso available as `getConstants()`\n"}