【问题标题】:Integrating Flow JS with Ember JS - Data not Binded将 Flow JS 与 Ember JS 集成 - 数据未绑定
【发布时间】:2015-01-17 16:03:42
【问题描述】:

我正在尝试将 FlowJS 与 EmberJS 集成。我成功地将数据上传到服务器,所以,我在这方面做得很好。

尝试将流对象数据与 emberJS 组件绑定以通过把手显示数据时失败。由于某种原因,数据绑定无法正常工作。

这是代码示例。

HTML

<script type="text/x-handlebars" data-template-name="index">
    {{flow-uploader}}
</script>

<script type="text/x-handlebars" id="components/flow-uploader">
    <div class="drop"></div>
    <!-- file list -->
    {{#each file in flow.files}}
        <p>File name: {{file.name}}</p>
    {{/each}}
</script>      

JS

App = Ember.Application.create({
    rootElement: '#main'
});


App.FlowUploaderComponent = Ember.Component.extend({

        flow: function(){
            return new Flow({
                target: 'http://google.com',
                testChunks: false
            });
        }.property(),

        initDropZone: function(){

            var flow = this.get('flow');
            var drop = this.$('.drop')[0];
            var $this = this;

            flow.assignDrop(drop);

            /*flow.on('fileAdded', function(file, flow){

            });
            flow.on('filesAdded', function(files){
            });
            flow.on('fileSuccess', function(file,message){
                console.log('file success');
            });
            flow.on('fileError', function(flow, message, chunk){
                console.log('file error');
            });*/
            flow.on('filesSubmitted', function(){
                console.log($this.get('flow').files);
                //flow.upload();
            });

            /*flow.on('complete', function(event, flow){
                console.log('completed');
            });

            flow.on('uploadStart', function(event, flow){
                console.log('uploading..');
            });
            flow.on('fileProgress', function(flow, file){

            });*/

        }.on('didInsertElement')

    });

示例可以在http://jsfiddle.net/sisir/qLuobq48/2/现场查看

基本上我在这里所做的是将流对象保存为组件属性。流对象的files属性是要上传的文件数组。一旦我们拖放或选择多个文件上传,files 数组就会更新。我们可以在控制台上看到它。日志代码通过filesSubmitted 事件添加。

从车把表达式中,每个文件都从文件队列中迭代。最初它是空的,但后来当它被填充时,它不会显示在 html 上。 数据绑定不工作出于某种原因。

【问题讨论】:

    标签: ember.js flow-js


    【解决方案1】:

    在你的组件逻辑中:

    App.FlowUploaderComponent = Ember.Component.extend({
    
        flow: function(){
            return new Flow({
                target: 'http://google.com',
                testChunks: false
            });
        }.property(),
    
        initDropZone: function(){
    
            var $this = this;    
    
            var flow = this.get('flow');
            var drop = this.$('.drop')[0];
    
            flow.assignDrop(drop);
    
            flow.on('filesSubmitted', function(){
    
              //for setting a single property
              $this.set('flowFiles', flow.files);
    
              //for setting multiple properties
              // $this.setProperties({'flowFiles': flow.files, /* etc.. */});
    
            });
    
    
        }.on('didInsertElement')
    
    });
    

    在您的模板中:

    <script type="text/x-handlebars" data-template-name="index">   
        {{flow-uploader}}
    </script>
    
    <script type="text/x-handlebars" id="components/flow-uploader">
         <div class="drop"></div>
        <!-- file list -->
        {{#each file in flowFiles}}
            File name: {{file.name}}
        {{/each}}
    </script>
    

    JSBin Example

    【讨论】:

    • 谢谢伙计!您的回答使我朝着正确的方向前进。实际上不得不采取一些不同的方法。我将不得不写一个答案。再次非常感谢它!
    【解决方案2】:

    您的 JSFiddle 的问题只是 (/was) 您将 text/plain 文件引用为 JavaScript。正如我asked here 一样,并不是每个文件都可以在 JSFiddle 中使用,而且 GitHub 不喜欢被误用作懒人的伪 CDN。

    换句话说:这个错误

    Uncaught ReferenceError: Flow is not defined
    

    是这个错误的结果

    Refused to execute script from 'https://raw.githubusercontent.com/flowjs/flow.js/master/dist/flow.min.js' because its MIME type ('text/plain') is not executable, and strict MIME type checking is enabled.
    

    并且可以使用

    来修复
    https://rawgithub.com
    

    或 MaxCDN 版本

    http://rawgit.com/
    

    代替分支文件 URl。

    查看the new JSFiddle 和所有console.log() 语句。控制台中的files addedfile addedsubmitted 日志告诉我,一旦您使用了正确的文件,您的代码中的所有内容都可以正常工作。专业提示:始终检查您的控制台是否有错误。

    【讨论】:

    • 原始文件在 Firefox 上为我工作。虽然没有在其他浏览器上检查它。 感谢您的提示。控制台和文件数组没问题。主要问题是为什么它不反映在车把上(数据绑定)。 IE。文件列表应出现在拖放区下方。
    猜你喜欢
    • 2021-12-16
    • 2019-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多