Skip to content

Instantly share code, notes, and snippets.

@tim-evans
Last active January 5, 2018 18:38
Show Gist options
  • Save tim-evans/eaaa749d55feb087e4444ea1d6544c82 to your computer and use it in GitHub Desktop.
Save tim-evans/eaaa749d55feb087e4444ea1d6544c82 to your computer and use it in GitHub Desktop.
Promise / Task based asset adding workflow
import Service from '@ember/service';
import { task } from 'ember-concurrency';
import { get, set } from '@ember/object';
import RSVP from 'rsvp';
export default Service.extend({
options: null,
findAsset: task(function* (afmConfig) {
set(this, 'options', Object.assign(RSVP.defer(), { afmConfig });
try {
yield get(this, 'options.promise');
} finally {
set(this, 'options', null);
}
}),
execute(afmConfig) {
return get(this, 'findAsset').perform(afmConfig);
}
});
{{#if isFindingAsset}}
{{take-over (component 'content-finder'
afmConfig=findAsset.options.afmConfig
submit=(action addAsset.options.resolve)
)
ondismiss=(action addAsset.options.reject)}}
{{/if}}
import Component from '@ember/component';
import { inject } from '@ember/service';
import { get } from '@ember/object';
import { notEmpty } from '@ember/object/computed';
export default Component.extend({
addAsset: service(),
isFindingAsset: notEmpty('findAsset.options')
});
import Route from '@ember/routing/route';
import method from 'ember-service-methods/inject';
export default Route.extend({
addAsset: method(),
actions: {
addAsset(model, field) {
return this.addAsset(this.getAfmConfigFor(model, field)).then(function () {
// add asset
}, function () {});
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment