设置模型后,骨干模型更改事件未触发

时间:2013-08-20 02:19:40

标签: jquery backbone.js

我采用非犹太方法并使用$.ajax()进行ajax文件上传,因为目前我无法找出“骨干”方式。图片已上传,ajax请求以JSON格式发回新骨干模型,并填充新照片的文件名。但是,该模型未触发change事件,因此无法填充个人资料图片。请参阅下面的success功能。

    getFile:function(e){
        e.preventDefault();
        that = this;
        var file = e.target.files[0];
        name = file.name;
        size = file.size;
        type = file.type;
        var formData = new FormData($('#upload-child-pic-form')[0]);
        console.log(this,'currentUploadU',this.currentUpload);
        formData.append('child_id',this.currentUpload);
        $.ajax({
            url: '/children/upload/',
            type: 'POST',
            xhr: function() { 
            var myXhr = $.ajaxSettings.xhr();
                if(myXhr.upload){ 
                    myXhr.upload.addEventListener('progress',function(data){
                        $("#upload-child-pic-progress").val(data.position / data.totalSize * 100);
                    }, false); 
                }
                return myXhr;
            },
            //Ajax events
            beforeSend: function(data){
                $("#upload-child-pic-progress").removeClass('hide');
            },
            success: function(data){
                $("#upload-child-pic-progress").addClass('hide');
                $("#add-child-profile-pics-modal").modal('hide');
                var sentData = $.parseJSON(data);
                var thisChild = that.children.get(sentData.id);
                thisChild.set("photo",sentData.photo);
                thisChild.trigger('change');
            },
            error: function(data){
                console.log('error',data);
            },
            // Form data
            data: formData,
            //Options to tell jQuery not to process data or worry about content-type.
            cache: false,
            contentType: false,
            processData: false
        });
    },

1 个答案:

答案 0 :(得分:0)

你在变量和其他变量之前缺少var ......

将这些变量设置为全局是一种威胁,在调用成功回调之前,可以在此范围之外更改/覆盖它们。

that = this; //should be - 'var that = this;'
相关问题