Last active
January 5, 2018 18:38
-
-
Save tim-evans/eaaa749d55feb087e4444ea1d6544c82 to your computer and use it in GitHub Desktop.
Promise / Task based asset adding workflow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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