{"id":19050899,"url":"https://github.com/tomasbasham/ember-cli-dispatch","last_synced_at":"2025-04-24T01:22:56.701Z","repository":{"id":57223222,"uuid":"50777728","full_name":"tomasbasham/ember-cli-dispatch","owner":"tomasbasham","description":"An ember-cli addon to extend computed properties with asynchronous values","archived":false,"fork":false,"pushed_at":"2022-12-06T22:14:03.000Z","size":920,"stargazers_count":10,"open_issues_count":5,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-14T16:48:15.802Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tomasbasham.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-01-31T14:14:04.000Z","updated_at":"2019-02-18T12:54:40.000Z","dependencies_parsed_at":"2023-01-24T11:31:44.250Z","dependency_job_id":null,"html_url":"https://github.com/tomasbasham/ember-cli-dispatch","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomasbasham%2Fember-cli-dispatch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomasbasham%2Fember-cli-dispatch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomasbasham%2Fember-cli-dispatch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomasbasham%2Fember-cli-dispatch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tomasbasham","download_url":"https://codeload.github.com/tomasbasham/ember-cli-dispatch/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250541751,"owners_count":21447574,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-08T23:16:38.690Z","updated_at":"2025-04-24T01:22:56.670Z","avatar_url":"https://github.com/tomasbasham.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ember-cli-dispatch [![Build Status](https://travis-ci.com/tomasbasham/ember-cli-dispatch.svg?branch=master)](https://travis-ci.com/tomasbasham/ember-cli-dispatch) [![Maintainability](https://api.codeclimate.com/v1/badges/55f72c38f385fc50973f/maintainability)](https://codeclimate.com/github/tomasbasham/ember-cli-dispatch/maintainability)\n\nAn [Ember CLI](https://ember-cli.com/) addon to extend computed properties with\nasynchronous values.\n\nComputed properties are really handy for taking one or more static properties,\nor in fact other computed properties, and transforming their values. However it\nis often the case that a value is not immediately present and may need to be\nfetch asynchronously. This prevents us from using computed properties because\nwe cannot return a future value. To accomplish this currently we could return a\npromise from a computed property and create, potentially, a long chain of\n`then`'s and `catch`'s to then finally compute the value. This is very\ncumbersome and will not lend itself well if required within template logic.\n\nThis addon implements a computed property macro that works internally with\npromises. Each computed property can be assigned a default value that will be\nused until the promise resolves and the computed property's value is updated.\n\n## Compatibility\n\n* Ember.js v2.18 or above\n* Ember CLI v2.13 or above\n\n## Installation\n\nFrom within your Ember CLI project directory run:\n```\nember install ember-cli-dispatch\n```\n\n## Usage\n\nThis addon implements a utility function wrapping a promise object, which once\nresolved will set the value of a computed property to that returned by the\npromise.\n\n### Computed Promise\n\nIn order to create an asynchronous computed property you must import the\n`computedPromise` macro included in this addon. The macro accepts a property\nfunction, that may return a promise, and optionally a default value that will\nbe used until the internal promise resolves.\n\n##### Computed Promise Example\n\n```JavaScript\n// app/controllers/application.js\nimport Controller from '@ember/controller';\nimport $ from 'jquery';\n\nimport computedPromise from 'ember-cli-dispatch/utils/computed-promise';\n\nexport default Controller.extend({\n  serverResponse: computedPromise(function() {\n    return $.getJSON('https://www.example.com/response.json')\n  }, 'fetching...')\n});\n```\n\nThis will create a computed property named `serverResponse` that will take on\nthe value `fetching...` until the result of the promise returned from jQuery's\n`getJson` method is resolved. Once resolved the computed property will equal\nthe value returned by the promise.\n\n### Bundled Macros\n\nIn addition to the `computedPromise` macro this addon includes a couple of\nutility macros that make use of wrapping computationally expensive operations\nwithin promises. The first of these utility macros is `map` which transforms\nevery value within an array; and the second is `reduce` which applies a\nfunction against an accumulator for each value of an array (from left-to-right)\nto reduce it to a single value.\n\n##### Computed Map Example\n\n```JavaScript\n// app/controllers/application.js\nimport Controller from '@ember/controller';\nimport computedMap from 'ember-cli-dispatch/utils/computed-map';\n\nexport default Controller.extend({\n  scores: [1, 2, 3, 4, 5],\n\n  doubleScores: computedMap('scores', function(score) {\n    return score * 2;\n  }, 'computing...')\n});\n```\n\nThis is a contrived example to create a computed property that doubles each of\nthe values within the `scores` array.\n\n##### Computed Reduce Example\n\n```JavaScript\n// app/controllers/application.js\nimport Controller from '@ember/controller';\nimport computedReduce from 'ember-cli-dispatch/utils/computed-reduce';\n\nexport default Controller.extend({\n  scores: [1, 2, 3, 4, 5],\n\n  totalScores: computedReduce('scores', function(cumulativeTotal, score) {\n    return cumulativeTotal + score;\n  }, 'computing...')\n});\n```\n\nThis is a contrived example to create a computed property that totals up the\nvalues within the `scores` array.\n\n### Custom Macro\n\nIt is also possible to create your own asynchronous computed property macros.\nTo do this you need to import the `computedPromise` macro.\n\n##### Custom Macro Example\n\n```JavaScript\n// app/utils/computed-macro.js\nimport computedPromise from 'ember-cli-dispatch/utils/computed-promise';\nimport { get } from '@ember/object';\n\nexport default function computedMacro(propertyName, defaultValue) {\n  return computedPromise(`${propertyName}.[]`, function() {\n    return get(this, propertyName);\n  }, defaultValue);\n};\n```\n\nThis shows a very basic implementation of a computed property macro that passes\nback the value of the specified property. This of course can be extended to\nperform whatever operation is required by your applications.\n\n## License\n\nThis project is licensed under the [MIT License](LICENSE.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomasbasham%2Fember-cli-dispatch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftomasbasham%2Fember-cli-dispatch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomasbasham%2Fember-cli-dispatch/lists"}