javascript - Ember.js 值与 HTML5 文件上传绑定(bind)

标签 javascript html ember.js

我离使用 Ember 数据上传文件不远了。但我没有得到正确的值(value)约束。相关代码下方。

这是 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]);
    }
});

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>

如您在 HTML 部分所见,我尝试将图像内容绑定(bind)到 Landcode 模型属性图像。没有大写 L 也试过了。

我想我不能这样绑定(bind)图像,因为它是一个自定义 View 对象?而且我认为通常它会自动绑定(bind)。也许我只是把一些事情做了两次。

引用资料:

  1. http://emberjs.com/api/classes/Ember.Binding.html

  2. http://devblog.hedtek.com/2012/04/brief-foray-into-html5-file-apis.html

  3. File upload with Ember data

  4. How: File Upload with ember.js

  5. http://discuss.emberjs.com/t/file-uploads-is-there-a-better-solution/765

  6. http://chrismeyers.org/2012/06/12/ember-js-handlebars-view-content-inheritance-image-upload-preview-view-object-binding/

最佳答案

我将您的代码更新为以下内容:

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]);
    }
});

App.UploadFile 中,我没有直接引用 Controller ,而是设置了 file 属性。所以你可以绑定(bind)你的模型属性,使用 View :

{{view App.UploadFile name="image" file=image }}

Ember.run 用于您在测试应用程序时没有问题。

请查看那个 jsfiddle http://jsfiddle.net/marciojunior/LxEsF/

只需填写输入并单击保存按钮。您将在浏览器控制台中看到将发送到服务器的数据。

关于javascript - Ember.js 值与 HTML5 文件上传绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19909267/

相关文章:

javascript - 为什么 jQuery 类不适用于 AJAX 响应生成的 HTML 元素?

javascript - 在 angular/material2 中使用 $primary scss 变量

javascript - 设置 Google 文档插件侧边栏的宽度不起作用

java - 为什么 Selenium 中的 getText() 不适用于 &lt;textarea&gt; 元素,但 getAttribute ("value")是?

javascript - Angular.js - 两个并发但互斥的路由 ($routeProvider)

javascript - 选择下拉列表后动态输入字段未正确显示

javascript - CSS-3 转换错误,菜单出现卡住

ember.js - 如何在 Ember.js 中删除模型的 hasMany 关联中的所有记录

ember.js - 从父路由继承模型

binding - Ember : re-bind a function to a view property (checkbox observing text area)