Skip to content

Instantly share code, notes, and snippets.

@chrism
Created May 23, 2016 10:08
Show Gist options
  • Save chrism/9d8034b9d76e4a1565d3f791a6b7ba27 to your computer and use it in GitHub Desktop.
Save chrism/9d8034b9d76e4a1565d3f791a6b7ba27 to your computer and use it in GitHub Desktop.
Working Ember 2.4.3
import Ember from 'ember';
const {
run: {
bind
}
} = Ember;
export default Ember.Component.extend({
label: 'choose a file',
didInsertElement() {
this.$().on('change', bind(this,'filesSelected'));
},
willDestroyElement() {
this.$().off('change', bind(this,'filesSelected'));
},
filesSelected(event) {
const files = event.target.files;
if (files.length === 0) {
Ember.Logger.log('you must add a file');
} else if (files.length > 0) {
this.sendAction('action', files);
}
}
});
/* global Blob */
import Ember from 'ember';
function createFile(content = ['test'], options = {}) {
const {
name,
type
} = options;
const file = new Blob(content, {type : type ? type : 'text/plain'});
file.name = name ? name : 'test.txt';
return file;
}
export default Ember.Test.registerAsyncHelper('uploadFile', function(app, selector, content, options) {
const file = createFile(content, options);
return triggerEvent(
selector,
'change',
{ target: { files: [file] } }
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment