Ember.js值与HTML5文件上传绑定

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也尝试过它.

我想我不能这样绑定图像,因为它是一个自定义视图对象?而且通常它会自动绑定我认为.也许我只做两次事情.

参考文献:

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

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

  3. 使用Ember数据上传文件

  4. 方法:使用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/

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/

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

  • 别客气!谢谢你指出这个错误 (2认同)