Forked from chrism/components.async-image-wrapper.js
Last active
July 26, 2017 14:45
-
-
Save javlc/2bda740443434afaf0db61dca6f41a18 to your computer and use it in GitHub Desktop.
registerWaiter on component directly
This file contains 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 Ember from 'ember'; | |
export default Ember.Component.extend({ | |
imageLoaded: 'image loading', | |
imageWidth: 'width loading', | |
// purely to make sure image loads each time and | |
// is not cached for testing | |
volatileSrc: Ember.computed('src', function(){ | |
return this.get('src') + '?cache=' + new Date().toISOString(); | |
}), | |
actions: { | |
imageLoaded(message, width) { | |
if (!this.isDestroyed) { | |
this.set('imageLoaded', message); | |
this.set('imageWidth', width); | |
} | |
} | |
} | |
}); |
This file contains 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 Ember from 'ember'; | |
export default Ember.Component.extend({ | |
tagName: 'img', | |
attributeBindings: ['src'], | |
init() { | |
this._super(...arguments); | |
if (Ember.testing) { | |
this._loading = false; | |
Ember.Test.registerWaiter(() => this._loading === false); | |
} | |
}, | |
load(src) { | |
let promise = new Ember.RSVP.Promise((resolve, reject) => { | |
const img = new Image(); | |
img.src = src; | |
img.onload = e => { | |
Ember.run(() => resolve(img)); | |
}; | |
img.onerror = () => { | |
Ember.run(() => reject()); | |
}; | |
}); | |
if (Ember.testing) { | |
this._loading = true; | |
return promise.finally(() => this._loading = false); | |
} | |
return promise; | |
}, | |
loadImage() { | |
let src= this.get('src'); | |
this.load(src).then(img => { | |
this.get('onImageLoad')("image loaded", img.width); | |
}) | |
.catch(error => { | |
Ember.Logger.log('error', error); | |
this.get('onImageLoad')("image could not load"); | |
}); | |
}, | |
didInsertElement() { | |
Ember.run.scheduleOnce('afterRender', this, this.loadImage); | |
} | |
}); |
This file contains 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 Ember from 'ember'; | |
export default Ember.Controller.extend({ | |
}); |
This file contains 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
body { | |
margin: 12px 16px; | |
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; | |
font-size: 12pt; | |
} | |
h1, h2 { | |
//display: none; | |
} |
This file contains 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 { test } from 'qunit'; | |
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance'; | |
moduleForAcceptance('Waits for image to be loaded'); | |
test('/', function(assert) { | |
visit('/'); | |
andThen(function() { | |
assert.equal(currentURL(), '/'); | |
assert.equal(find('#image-loaded-text').text(), 'image loaded'); | |
assert.equal(find('#image-width-text').text(), '412'); | |
}); | |
}); |
This file contains 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 Ember from 'ember'; | |
export default function destroyApp(application) { | |
Ember.run(application, 'destroy'); | |
} |
This file contains 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 { module } from 'qunit'; | |
import Ember from 'ember'; | |
import startApp from '../helpers/start-app'; | |
import destroyApp from '../helpers/destroy-app'; | |
const { RSVP: { Promise } } = Ember; | |
export default function(name, options = {}) { | |
module(name, { | |
beforeEach() { | |
this.application = startApp(); | |
if (options.beforeEach) { | |
return options.beforeEach.apply(this, arguments); | |
} | |
}, | |
afterEach() { | |
let afterEach = options.afterEach && options.afterEach.apply(this, arguments); | |
return Promise.resolve(afterEach).then(() => destroyApp(this.application)); | |
} | |
}); | |
} |
This file contains 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 Resolver from '../../resolver'; | |
import config from '../../config/environment'; | |
const resolver = Resolver.create(); | |
resolver.namespace = { | |
modulePrefix: config.modulePrefix, | |
podModulePrefix: config.podModulePrefix | |
}; | |
export default resolver; |
This file contains 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 Ember from 'ember'; | |
import Application from '../../app'; | |
import config from '../../config/environment'; | |
const { run } = Ember; | |
const assign = Ember.assign || Ember.merge; | |
export default function startApp(attrs) { | |
let application; | |
let attributes = assign({rootElement: "#test-root"}, config.APP); | |
attributes = assign(attributes, attrs); // use defaults, but you can override; | |
run(() => { | |
application = Application.create(attributes); | |
application.setupForTesting(); | |
application.injectTestHelpers(); | |
}); | |
return application; | |
} |
This file contains 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 { moduleForComponent, test } from 'ember-qunit'; | |
import wait from 'ember-test-helpers/wait'; | |
import hbs from 'htmlbars-inline-precompile'; | |
moduleForComponent('async-image-wrapper', 'Uses Promise', { | |
integration: true | |
}); | |
test('If image found update message to success', function(assert) { | |
assert.expect(2); | |
this.render(hbs`{{async-image-wrapper src='https://emberjs.com/images/tomsters/sandiego-zoey-b7637d09.png'}}`); | |
return wait().then( () => { | |
assert.equal(this.$('#image-loaded-text').text(), 'image loaded'); | |
assert.equal(this.$('#image-width-text').text(), '412'); | |
}); | |
}); |
This file contains 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 resolver from './helpers/resolver'; | |
import { | |
setResolver | |
} from 'ember-qunit'; | |
setResolver(resolver); |
This file contains 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
{ | |
"version": "0.10.4", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": true | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js", | |
"ember": "2.6.0", | |
"ember-data": "2.6.0", | |
"ember-template-compiler": "2.6.0" | |
}, | |
"addons": {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment