项目作者: buschtoens

项目描述 :
Easily access tasks defined on a route in your templates
高级语言: JavaScript
项目地址: git://github.com/buschtoens/ember-route-task-helper.git
创建时间: 2017-08-03T20:52:26Z
项目社区:https://github.com/buschtoens/ember-route-task-helper

开源协议:MIT License

下载


ember-route-task-helper

Build Status
npm version
Download Total
Ember Observer Score
code style: prettier
Dependabot enabled
dependencies Status
devDependencies Status

The route-task template helper allows you to easily access tasks defined on a
route in the currently active route hierarchy. Essentially this addon is just
like ember-route-action-helper but for
ember-concurrency tasks.

Installation

  1. ember install ember-route-task-helper

This addon will work on Ember versions 2.4.x and up only, due to use of the
new RouterService. If your Ember version does not natively support it yet, you
need to install the polyfill:

  1. ember install ember-router-service-polyfill

Of course, you need to have ember-concurrency
installed. If you haven’t already, run this command first:

  1. ember install ember-concurrency

Minimum required version is 0.6.x.

If you want to use this addon inside an engine, you’ll need to have an
EngineRouterService. You can use my PoC linked in
ember-engines#587 until it’s released officially as
part of ember-engines.

Usage

Template Helper: (route-task taskName ...curryArguments)

Wherever in a template you would access a task by its name, replace it with
(route-task "taskName") and move that task to a route. For instance:

  1. {{task-button
  2. task=myTask
  3. }}
  4. {{!-- now becomes --}}
  5. {{task-button
  6. task=(route-task "myTask")
  7. }}

Notice the quotes around "myTask".

You can also curry your tasks:

  1. {{task-button
  2. task=(task myTask "Freddie" "Morecurry")
  3. }}
  4. {{!-- now becomes --}}
  5. {{task-button
  6. task=(route-task "myTask" "Freddie" "Morecurry")
  7. }}

Exemplary Migration

Let’s start with a traditional task defined on a component.

  1. // app/components/delete-user.js
  2. import Component from '@ember/component';
  3. import { get } from '@ember/object';
  4. import { task, timeout } from 'ember-concurrency';
  5. export default Component.extends({
  6. /**
  7. * A User record.
  8. * @type {DS.Model}
  9. */
  10. user: null,
  11. /**
  12. * Deletes the user after a timeout of 5 seconds.
  13. * @type {Task}
  14. */
  15. deleteUser: task(function*() {
  16. timeout(5000); // give the user time to think about it
  17. yield get(this, 'user').destroyRecord();
  18. }).drop()
  19. });
  1. {{!-- app/templates/components/delete-user.hbs --}}
  2. {{#if deleteUser.isIdle}}
  3. <button onclick={{perform deleteUser}}>
  4. Delete {{user.name}}
  5. </button>
  6. {{else}}
  7. Deleting {{user.name}} in 5 seconds. You can still abort.
  8. <button onclick={{cancel-all deleteUser}}>
  9. Nah, spare their life.
  10. </button>
  11. {{/if}}

And now we’ll move it to a route, shall we?

  1. // app/routes/user.js
  2. import Route from '@ember/routing/route';
  3. import { get } from '@ember/object';
  4. import { task, timeout } from 'ember-concurrency';
  5. export default Route.extends({
  6. /**
  7. * Deletes the current model after a timeout of 5 seconds.
  8. * @type {Task}
  9. */
  10. deleteUser: task(function*() {
  11. timeout(5000); // give the user time to think about it
  12. const user = this.modelFor(this.routeName);
  13. yield user.destroyRecord();
  14. }).drop()
  15. });
  1. {{!-- app/templates/user.hbs --}}
  2. {{#if (get (route-task "deleteUser") "isIdle")}}
  3. <button onclick={{perform (route-task "deleteUser")}}>
  4. Delete {{model.name}}
  5. </button>
  6. {{else}}
  7. Deleting {{model.name}} in 5 seconds. You can still abort.
  8. <button onclick={{cancel-all (route-task "deleteUser")}}>
  9. Nah, spare their life.
  10. </button>
  11. {{/if}}

Currying

  1. // app/routes/user.js
  2. import Route from '@ember/routing/route';
  3. import { get } from '@ember/object';
  4. import { task, timeout } from 'ember-concurrency';
  5. export default Route.extends({
  6. /**
  7. * Deletes the current model after a timeout of 5 seconds.
  8. * @type {Task}
  9. */
  10. deleteUser: task(function*(user) {
  11. timeout(5000); // give the user time to think about it
  12. yield user.destroyRecord();
  13. }).drop()
  14. });
  1. {{!-- app/templates/user.hbs --}}
  2. {{task-button
  3. task=(route-task "deleteUser" model)
  4. idleLabel=(concat "Delete " model.name)
  5. runningLabel="Cancel deletion"
  6. }}

Notes on Syntax

I personally dislike repeating (route-task) a bunch of times in my templates or even worse having to use the (get) helper to derive state from a task. You can avoid that by either only passing a task to a component (as shown in the {{task-button}} example above) or by using the {{with}} helper:

  1. {{#with (route-task "deleteUser" model) as |deleteUser|}}
  2. {{#if deleteUser.isIdle}}
  3. <button onclick={{perform deleteUser}}>
  4. Delete {{model.name}}
  5. </button>
  6. {{else}}
  7. Deleting {{model.name}} in 5 seconds. You can still abort.
  8. <button onclick={{cancel-all deleteUser}}>
  9. Nah, spare their life.
  10. </button>
  11. {{/if}}
  12. {{/with}}

Util: routeTask(context, taskName, ...curryArguments)

There also is a routeTask util, that’s really similar to ember-invoke-action and might come in handy for JS heavy components.

  1. // app/components/delete-user.js
  2. import Component from '@ember/component';
  3. import { get } from '@ember/object';
  4. import { routeTask } from 'ember-route-task-helper';
  5. export default Component.extends({
  6. /**
  7. * A User record.
  8. * @type {DS.Model}
  9. */
  10. user: null,
  11. /**
  12. * Deletes the user after a timeout of 5 seconds.
  13. */
  14. click() {
  15. const user = get(this, 'user');
  16. routeTask(this, 'deleteUser').perform(user);
  17. }
  18. });

Currying

As with the (route-task) helper, you can curry the task with as many arguments as you like. So the above is interchangeable with:

  1. click() {
  2. const user = get(this, 'user');
  3. routeTask(this, 'deleteUser', user).perform();
  4. }

routeTaskFromRouter(router, taskName, ...curryArguments)

Internally routeTask performs a lookup for the Router everytime you call it. If you already happen to have the router instance available in your current scope, you could also pass it directly to skip the lookup:

  1. // app/components/delete-user.js
  2. import Component from '@ember/component';
  3. import { get, computed } from '@ember/object';
  4. import { getOwner } from '@ember/application';
  5. import { routeTaskFromRouter } from 'ember-route-task-helper';
  6. export default Component.extends({
  7. /**
  8. * A User record.
  9. * @type {DS.Model}
  10. */
  11. user: null,
  12. /**
  13. * The app's router instance.
  14. * @type {Ember.Router}
  15. */
  16. router: computed(function() {
  17. return getOwner(this).lookup('main:router');
  18. }).readOnly(),
  19. /**
  20. * Deletes the user after a timeout of 5 seconds.
  21. */
  22. click() {
  23. const router = get(this, 'router');
  24. const user = get(this, 'user');
  25. routeTaskFromRouter(router, 'deleteUser').perform(user);
  26. }
  27. });

Notes on DDAU

In my opinion, using routeTask in components generally isn’t a good design pattern. I would much rather prefer to explicitly pass the route task as an attribute:

  1. {{my-component taskName=(route-task "taskName")}}

Just by looking at the component invocation in the template, the user should be able to judge what’s going in and what’s coming out of a component (DDAU). This way components remain completely agnostic and make no assumptions about the environment they are invoked in.

Calling routeTask inside a component is really non-transparent and promotes an unhealthy invisible entanglement.

On the other hand, you can already call (route-task) in the component’s template.

I’ve implemented it for feature parity. But just because it’s there, doesn’t mean you have to use it. But don’t let me stop you. :stuck_out_tongue_winking_eye:

Contributing

I sincerely hope this addon serves you well. Should you encounter a bug, have a great idea or just a question, please do open an issue and let me know. Even better yet, submit a PR yourself! :blush:

This addon is using the Prettier code formatter. It’s embedded as a fixable eslint rule. If you’re editor is set up to fix eslint errors on save, you’re code is auto formatted. If not, please make sure you’re not getting any linter errors.

Attribution

The original idea for this addon was brought up in machty/ember-concurrency#89 by @Luiz-N. This addon is in many ways a straight copy and paste from ember-route-action-helper by @DockYard.

A huge thank you to goes out to @machty for developing ember-concurrency in the first place. :heart: