Skip to content

Instantly share code, notes, and snippets.

@neborn
Last active February 14, 2021 01:35
Show Gist options
  • Save neborn/c9bd399953af7f106d5079ce51422c5d to your computer and use it in GitHub Desktop.
Save neborn/c9bd399953af7f106d5079ce51422c5d to your computer and use it in GitHub Desktop.
Passing Complex Action Arguments
import Component from '@glimmer/component';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';
export default class ContainerComponent extends Component {
@service
api;
@action
performAsyncAction(willSucceed) {
// This function would likely include additional request and response decoration logic, along with possibly its own side effects.
return this.api.makeCall(willSucceed);
}
}
import Component from '@glimmer/component';
import { action } from '@ember/object';
import { tracked } from '@glimmer/tracking';
export default class extends Component {
@tracked
buttonEnabled = true;
@tracked
errorMessage = '';
@tracked
successMessage = '';
@action
async handleClick(resolve = true) {
this.errorMessage = '';
this.successMessage = '';
this.buttonEnabled = false;
try {
await this.args.onClick(resolve);
} catch (e) {
if (!this.isDestroying && !this.isDestroyed) {
this.errorMessage = 'All your error are belong to us';
}
return;
} finally {
if (!this.isDestroying && !this.isDestroyed) {
this.buttonEnabled = true;
}
}
if (this.isDestroying || this.isDestroyed) {
return;
}
this.successMessage = 'It worked!';
}
}
import Service from '@ember/service';
import { action } from '@ember/object';
const TIMEOUT = 1000;
export default class ApiService extends Service {
@action
makeCall(willSucceed) {
return new Promise((resolve, reject) => {
const callback = willSucceed ?
() => resolve([true, 200]) :
() => reject([false, 500]);
setTimeout(callback, TIMEOUT);
});
}
}
<p>
This is an example of what not to do.
Please do not copy.
See the following example for the recommended alternative: https://ember-twiddle.com/de9bf2df1f55dac9fed8c033ce8f7c78
</p>
<Container />
<Presentation @onClick={{this.performAsyncAction}} />
{{#if this.successMessage}}
<p>{{this.successMessage}}</p>
{{/if}}
{{#if this.errorMessage}}
<p>{{this.errorMessage}}</p>
{{/if}}
<button
type="button"
disabled={{if this.buttonEnabled false true}}
{{on "click" (fn this.handleClick true)}}
>
Perform async action resolve
</button>
<button
type="button"
disabled={{if this.buttonEnabled false true}}
{{on "click" (fn this.handleClick false)}}
>
Perform async action reject
</button>
{
"version": "0.17.1",
"EmberENV": {
"FEATURES": {},
"_TEMPLATE_ONLY_GLIMMER_COMPONENTS": false,
"_APPLICATION_TEMPLATE_WRAPPER": true,
"_JQUERY_INTEGRATION": true
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js",
"ember": "3.18.1",
"ember-template-compiler": "3.18.1",
"ember-testing": "3.18.1"
},
"addons": {
"@glimmer/component": "1.0.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment