Thanks for the code !
I was able to implement working component .
-components/x-file-upload.js
EmTasks.XFileUploadComponent = Ember.Component.extend({
actions: {
destroyAttachment: function(id) {
var store = this.get("targetObject.store");
if (confirm("Are you sure?")) {
store.find('attachment', id).then(function (attachment) {
attachment.destroyRecord()
});
}
}
},
didInsertElement: function() {
var self = this;
$('.file-uploader').fileupload({
dataType: 'json',
method: 'POST',
url: '/attachments',
paramName: 'file',
autoUpload: true,
add: function (e, data) {
//console.log("add: e, data", e, data);
data.submit();
},
submit: function (e, data) {
//console.log("jquery submit this, self", $(this), self, self.task);
},
done: function (e, data) {
var store = self.get("targetObject.store");
var attachment = data.result.attachment;
attachment.task = self.task;
store.push("attachment", attachment);
self.sendAction('saveComplete');
},
fail: function (e, data) {
console.log('failed', e);
self.sendAction('saveFailed');
}
});
},
willDestroyElement: function() {
$('.file-uploader').fileupload('destroy');
}
});
-templates/components/x-file-upload.handlebars
<span class="btn btn-success fileinput-button">
<form>
<i class="glyphicon glyphicon-plus"></i>
<span>Select files...</span>
<!-- The file input field used as target for the file upload widget -->
<input id="fileupload" class='file-uploader' type="file" name="files[]" multiple>
<input name="task_id" type='hidden' value='{{task.id}}'>
</form>
</span>
<br/>
<div class='col-md-8'>
<h4><i class="fa fa-file fa-lg"></i> Załączniki</h4>
</div>
{{#each task.attachments as |attachment|}}
<div class="col-md-8">
<div class='col-md-8'>
{{attachment.file_name}}
</div>
<a href='{{attachment.url}}' target='_blank'>
<i class="fa fa-download"></i> Ściągnij
</a>
<a {{action 'destroyAttachment' attachment.id }} class='clickable'>
<i class="fa fa-times"></i>Usuń
</a>
</div>
{{/each}}
<div class='col-md-12'>
<hr />
</div>
-usage in parent template
<div role="tabpanel" class="tab-pane" id="attachments">
{{x-file-upload task=model}}
</div>
Everything works:
- adding new file
- list of attachments is refreshed
- deletion also works
I didn’t have luck with any tutorials, too many things to change and they require installation various external things.
The bad news is that I don’t use Ember CLI, so 90% working libraries won’t work for me.
That’s funny, in Angular I can download raw source and re-use it.
But in Ember I have to obligatory use Ember CLI - I don’t like, I don’t want to install it to small/medium app. Too much hassle for this temp project.