Skip to content

Instantly share code, notes, and snippets.

@hbrysiewicz
Last active April 1, 2017 00:34
Show Gist options
  • Select an option

  • Save hbrysiewicz/423018386f8c448e2d00232bd3e3e635 to your computer and use it in GitHub Desktop.

Select an option

Save hbrysiewicz/423018386f8c448e2d00232bd3e3e635 to your computer and use it in GitHub Desktop.
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