Last active
April 1, 2017 00:34
-
-
Save hbrysiewicz/423018386f8c448e2d00232bd3e3e635 to your computer and use it in GitHub Desktop.
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 DS from 'ember-data' | |
| import Ember from 'ember' | |
| import { task } from 'ember-concurrency' | |
| const { | |
| Model, | |
| attr, | |
| belongsTo | |
| } = DS | |
| const { | |
| String: { htmlSafe }, | |
| computed, | |
| get, | |
| inject: { service }, | |
| set | |
| } = Ember | |
| export default Model.extend({ | |
| ajax: service(), | |
| url: attr('string'), | |
| svg: computed('url', '_svg', 'getSvg.isRunning', function() { | |
| let url = get(this, 'url') | |
| let svg = get(this, '_svg') | |
| let isRunning = get(this, 'getSvg.isRunning') | |
| // This is not an svg | |
| if (!url || !url.match(/\.svg$/)) { | |
| return null | |
| } | |
| if (!svg && !isRunning) { | |
| get(this, 'getSvg').perform(url) | |
| } | |
| return svg | |
| }), | |
| getSvg: task(function* (url) { | |
| yield get(this, 'ajax').request(url, { | |
| dataType: 'text' | |
| }).then((response) => { | |
| set(this, '_svg', htmlSafe(response)) | |
| }) | |
| }) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment