Del*_*ynx 5 javascript html5 ember.js
我与文件上传一起使用Ember-data并不远.但我没有得到正确的价值绑定.下面是相关代码.
这是App.js
App.LandcodeNewRoute = Ember.Route.extend({
model: function () {
return this.store.createRecord('landcode');
},
actions: {
saveLandcode: function () {
this.currentModel.save();
}
}
});
// REST & Model
App.ApplicationAdapter = DS.RESTAdapter.extend({
namespace: 'api'
});
App.Store = DS.Store.extend({
adapter: 'App.ApplicationAdapter'
});
App.Landcode = DS.Model.extend({
code: DS.attr('string'),
image: DS.attr('string')
});
// Views
App.UploadFile = Ember.TextField.extend({
tagName: 'input',
attributeBindings: ['name'],
type: 'file',
change: function (e) {
var reader, that;
that = this;
reader = new FileReader();
reader.onload = function (e) {
var fileToUpload = e.target.result;
console.log(e.target.result); // this spams the console with the image content
console.log(that.get('controller')); // output: Class {imageBinding: Binding,
that.get('controller').set(that.get('name'), fileToUpload);
};
return reader.readAsText(e.target.files[0]);
}
});
Run Code Online (Sandbox Code Playgroud)
HTML
<script type="text/x-handlebars" data-template-name="landcode/new">
Code: {{input value=code}}<br />
Image: {{view App.UploadFile name="image" imageBinding="Landcode.image" }}
<button {{action 'saveLandcode'}}>Save</button>
</script>
Run Code Online (Sandbox Code Playgroud)
正如您在HTML部分中看到的那样,我尝试将imagecontent绑定到Landcode模型属性图像.没有资本L也尝试过它.
我想我不能这样绑定图像,因为它是一个自定义视图对象?而且通常它会自动绑定我认为.也许我只做两次事情.
参考文献:
Mar*_*ior 10
我将您的代码更新为以下内容:
App.LandcodeNewRoute = Ember.Route.extend({
model: function () {
return this.store.createRecord('landcode');
},
actions: {
saveLandcode: function () {
this.currentModel.save();
}
}
});
// REST & Model
App.ApplicationAdapter = DS.RESTAdapter.extend({
namespace: 'api'
});
App.Landcode = DS.Model.extend({
code: DS.attr('string'),
image: DS.attr('string')
});
// views
App.UploadFile = Ember.TextField.extend({
tagName: 'input',
attributeBindings: ['name'],
type: 'file',
file: null,
change: function (e) {
var reader = new FileReader(),
that = this;
reader.onload = function (e) {
var fileToUpload = e.target.result;
Ember.run(function() {
that.set('file', fileToUpload);
});
};
return reader.readAsDataURL(e.target.files[0]);
}
});
Run Code Online (Sandbox Code Playgroud)
在App.UploadFile代替参考控制器directlly,我设置的file属性.因此,您可以使用以下命令绑定模型属性:
{{view App.UploadFile name="image" file=image }}
Run Code Online (Sandbox Code Playgroud)
本Ember.run是用来测试你应用程序时不会有问题.
请看一下jsfiddle http://jsfiddle.net/marciojunior/LxEsF/
只需填写输入并单击"保存"按钮.您将在浏览器控制台中看到将发送到服务器的数据.
| 归档时间: |
|
| 查看次数: |
6802 次 |
| 最近记录: |