javascript - 从类返回数据

标签 javascript jquery

我有一个文件放置类。用户将图像放入(任意数量),然后上传。

我从我的主类中调用该类:

this.fileDrop = new lx.FileDrop();

这是类(class):

(function(){
"use strict";

var FileDrop = function() {
    this.init();
};

p.init = function() {

    this._initEvents();

};

p._initEvents = function() {

    $(window).on('drop', this.onDrop.bind(this)).on('dragover', this.onDragOver);

};


p.onDrop = function(e) {

    e.preventDefault();
    var self = this;
    var files = e.originalEvent.dataTransfer.files;
    $.each(files, function(index, file){


        self.readFile(file).done(function(data) {

            //how to return the data?
        });

    });

};

p.onDragOver = function(e) {
    e.preventDefault();
};

p.readFile = function(file) {

    var fileReader = new FileReader();
    var deferred = $.Deferred();

    fileReader.onload = function(event) {

        deferred.resolve(event.target.result);
    };

    fileReader.onerror = function() {
        deferred.reject(this);
    };

    fileReader.readAsDataURL(file);

    return deferred.promise();

};


lx.FileDrop = FileDrop;
}(window));

我的问题涉及将图像数据返回到主类。我怎样才能返回?我如何存储它 - 我希望将返回到主类数组中的所有内容存储起来。上传多个图像时这将如何工作。会进行某种延期工作吗?

最佳答案

像这样怎么样:

var dfd = $.Deferred(),
    images = [];

$.each(files, function(index, file){

    self.readFile(file).done(function(data) {

        dfd.progress(data);
        images.push(data);
        if(files.length === ++index)
            dfd.resolve(images);

    }).fail(dfd.reject);
});

在任何你喜欢的地方处理延迟对象:

dfd.progress(function(file){
    console.log('file successfully uploaded', file);
}).done(function(images){
    console.log('all files successfully uploaded', images);
}).fail(function(){
    console.log('something went wrong while uploading an image');
});

另一个例子:

function FileDrop(){
    this.uploadCount = 0;
    this.images = [];
}

FileDrop.prototype.getImages = function(){

    var dfd = $.Deferred(),
        size = 3,
        that = this;

    for(var i = 0; i < size; i++){

        this.getImage(i*500, function(image){
            var dummyImage = $('<img/>'); 
                // should be 'image' from the callback in your case
            that.images.push(dummyImage);
            dfd.notify(dummyImage);
            if(that.uploadCount === size){
                dfd.resolve(that.images);
            }
        });
    }

    return dfd.promise();
};

FileDrop.prototype.getImage = function(timeout, callback){
    var that = this;
    setTimeout(function(){
        that.uploadCount++;
        callback();
    }, timeout);
};

var fd = new FileDrop();

fd.getImages().progress(function(image){
    console.log('progress', image);
}).done(function(imageArray){
    // preferred way: 
    //access the array when you know it's complete in the callback
    console.log('done', imageArray);
});

setTimeout(function(){
    // I think this is what you asked for, however, you must make an 
    // assumption when the images are completed, which is a bad idea
    console.log(fd.images);
}, 2000);

http://jsfiddle.net/Nm5vK/2/

关于javascript - 从类返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24034622/

相关文章:

javascript - 在 Jquery 中创建 Firebug 对象选择器工具

javascript - 如何获得最接近的 <li> 来设置 .css 高度

javascript - 如何在 Sublime Text 3 中将此代码用作 JS 的代码段?

javascript - 你如何使溢出的 float 元素居中?

javascript - 使用解析的日期获取后,Typescript 将 JSON 转换为对象

javascript - 使 jQuery 库在 Facebook.com 域中可用

javascript - 需要将对象数组中的对象分组为一组

javascript - jquery-datatables-rails。响应扩展和移动 View

javascript - Jquery on Input 不适用于动态插入的值

javascript - <autofocus> 在刷新页面之前无法重复获得焦点