私のアプリケーションで使用するアップロード ボタンは次のとおりです。入力ボタンを構築し、変更時に自動アップロードします。
{{view App.UploadButton groupBinding="model"}}
App.UploadButton = Ember.View.extend({
tagName: 'input',
attributeBindings: ['type'],
type: 'file',
originalText: 'Upload Finished Product',
uploadingText: 'Busy Uploading...',
newItemHandler: function (data) {
var store = this.get('controller.store');
store.push('item', data);
},
preUpload: function () {
var me = this.$(),
parent = me.closest('.fileupload-addbutton'),
upload = this.get('uploadingText');
parent.addClass('disabled');
me.css('cursor', 'default');
me.attr('disabled', 'disabled');
},
postUpload: function () {
var me = this.$(),
parent = me.closest('.fileupload-addbutton'),
form = parent.closest('#fake_form_for_reset')[0],
orig = this.get('originalText');
parent.removeClass('disabled');
me.css('cursor', 'pointer');
me.removeAttr('disabled');
form.reset();
},
change: function (e) {
var self = this;
var formData = new FormData();
// This is just CSS
this.preUpload();
formData.append('group_id', this.get('group.id'));
formData.append('file', this.$().get(0).files[0]);
$.ajax({
url: '/file_upload_handler/',
type: 'POST',
//Ajax events
success: function (data) { self.postUpload(); self.newItemHandler(data); },
error: function () { self.postUpload(); alert('Failure'); },
// Form data
data: formData,
//Options to tell jQuery not to process data or worry about content-type.
cache: false,
contentType: false,
processData: false
});
}
});