我需要创建一个Ember组件来选择一个文件.我的页面将包含多个"上传组件"
我已经阅读了一篇试图实现的帖子:(/sf/ask/644000031/)但是UploadFileView直接链接到控制器.我想要更通用的东西......
我想从视图中删除App.StoreCardController.set('logoFile'..)依赖项或从模板中传递字段(logoFile)...
有没有想过改进这段代码?
App.UploadFileView = Ember.TextField.extend({
type: 'file',
attributeBindings: ['name'],
change: function(evt) {
var self = this;
var input = evt.target;
if (input.files && input.files[0]) {
App.StoreCardController.set('logoFile', input.files[0]);
}
}
});
Run Code Online (Sandbox Code Playgroud)
和模板:
{{view App.UploadFileView name="icon_image"}}
{{view App.UploadFileView name="logo_image"}}
Run Code Online (Sandbox Code Playgroud)
Tor*_*ups 14
我完成了一个完整的例子来展示这一点
https://github.com/toranb/ember-file-upload
这是基本的车把模板
<script type="text/x-handlebars" data-template-name="person">
{{view PersonApp.UploadFileView name="logo" contentBinding="content"}}
{{view PersonApp.UploadFileView name="other" contentBinding="content"}}
<a {{action submitFileUpload content target="parentView"}}>Save</a>
</script>
Run Code Online (Sandbox Code Playgroud)
这是自定义文件视图对象
PersonApp.UploadFileView = Ember.TextField.extend({
type: 'file',
attributeBindings: ['name'],
change: function(evt) {
var self = this;
var input = evt.target;
if (input.files && input.files[0]) {
var reader = new FileReader();
var that = this;
reader.onload = function(e) {
var fileToUpload = reader.result;
self.get('controller').set(self.get('name'), fileToUpload);
}
reader.readAsDataURL(input.files[0]);
}
}
});
Run Code Online (Sandbox Code Playgroud)
这是控制器
PersonApp.PersonController = Ember.ObjectController.extend({
content: null,
logo: null,
other: null
});
Run Code Online (Sandbox Code Playgroud)
最后这里是视图w/submit事件
PersonApp.PersonView = Ember.View.extend({
templateName: 'person',
submitFileUpload: function(event) {
event.preventDefault();
var person = PersonApp.Person.createRecord({ username: 'heyo', attachment: this.get('controller').get('logo'), other: this.get('controller').get('other') });
this.get('controller.target').get('store').commit();
}
});
Run Code Online (Sandbox Code Playgroud)
如果您启动django应用程序,这将在文件系统上删除2个文件
lpi*_*.eu 10
编辑(2015.06):刚刚创建了一个基于组件的新解决方案.此解决方案提供带有预览和删除图标的上传按钮.PS这些fa类是Font Awesome
组件车把
<script type="text/x-handlebars" data-template-name='components/avatar-picker'>
{{#if content}}
<img src={{content}}/> <a {{action 'remove'}}><i class="fa fa-close"></i></a>
{{else}}
<i class="fa fa-picture-o"></i>
{{/if}}
{{input-image fdata=content}}
</script>
Run Code Online (Sandbox Code Playgroud)
组件JavaScript
App.AvatarPickerComponent = Ember.Component.extend({
actions: {
remove: function() {
this.set("content", null);
}
}
});
App.InputImageComponent = Ember.TextField.extend({
type: 'file',
change: function (evt) {
var input = evt.target;
if (input.files && input.files[0]) {
var that = this;
var reader = new FileReader();
reader.onload = function (e) {
var data = e.target.result;
that.set('fdata', data);
};
reader.readAsDataURL(input.files[0]);
}
}
});
Run Code Online (Sandbox Code Playgroud)
用法示例
{{avatar-picker content=model.avatar}}
Run Code Online (Sandbox Code Playgroud)
老答案
我拿了Chris Meyers的例子,我把它缩小了.
模板
{{#view Ember.View contentBinding="foto"}}
{{view App.FotoUp}}
{{view App.FotoPreview width="200" srcBinding="foto"}}
{{/view}}
Run Code Online (Sandbox Code Playgroud)
JavaScript的
App.FotoPreview= Ember.View.extend({
attributeBindings: ['src'],
tagName: 'img',
});
App.FotoUp= Ember.TextField.extend({
type: 'file',
change: function(evt) {
var input = evt.target;
if (input.files && input.files[0]) {
var that = this;
var reader = new FileReader();
reader.onload = function(e) {
var data = e.target.result;
that.set('parentView.content', data);
}
reader.readAsDataURL(input.files[0]);
}
},
});
Run Code Online (Sandbox Code Playgroud)