Skip to content

Instantly share code, notes, and snippets.

@H1D
Created March 28, 2014 09:41
Show Gist options
  • Save H1D/9828988 to your computer and use it in GitHub Desktop.
Save H1D/9828988 to your computer and use it in GitHub Desktop.
with file upload ember jquery-file-upload
<input type="file" class="fileupload__input" name="{{unbound name_attr}}">
<button class="fileupload__button" {{bind-attr disabled="disabled"}}>
{{#unless upload_progress}}
{{unbound text}}
{{else}}
{{upload_progress}}
{{/unless}}
</button>
App.FileUploadComponent = Em.Component.extend({
tagName: 'label',
classNames: ['fileupload__component'],
upload_progress: null,
disabled: Em.computed.bool('upload_progress'),
didInsertElement: function() {
Em.assert('should have an url', this.get('url'));
return this.$().fileupload({
url: this.get('url'),
done: (function(_this) {
return function(e, data) {
return _this.sendAction('done', data.result);
};
})(this),
fail: (function(_this) {
return function() {
return _this.sendAction('fail');
};
})(this),
start: (function(_this) {
return function() {
return _this.sendAction('start');
};
})(this),
progressall: (function(_this) {
return function(e, data) {
var progress;
progress = parseInt(data.loaded / data.total * 100, 10);
return _this.set('upload_progress', "" + progress + "%");
};
})(this)
});
},
willDestroyElement: function() {
return this.$().fileupload('destroy');
}
});
{{file-upload
name_attr="file"
url='/upload-url/'
text="select file"
done="submit_done"
fail="submit_fail"
start="upload_start"}}
{{!-- `submit_done`, `submit_done` and `upload_start` are action names you may want to handle in controller or route --}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment