Jquery 主干模型更改事件在设置模型后未触发
我采用了一种非犹太的方法,并使用$.ajax进行ajax文件上传,因为目前我还不知道如何上传。图片已经上传,ajax请求以JSON格式发回新的主干模型,并填充新图片的文件名。但是,模型没有触发更改事件,因此我无法填充配置文件图片。请参阅下面的成功函数Jquery 主干模型更改事件在设置模型后未触发,jquery,backbone.js,Jquery,Backbone.js,我采用了一种非犹太的方法,并使用$.ajax进行ajax文件上传,因为目前我还不知道如何上传。图片已经上传,ajax请求以JSON格式发回新的主干模型,并填充新图片的文件名。但是,模型没有触发更改事件,因此我无法填充配置文件图片。请参阅下面的成功函数 getFile:function(e){ e.preventDefault(); that = this; var file = e.target.files[0]; name
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
});
},
在该变量和其他变量之前缺少var 拥有这些全局变量是一种威胁,在调用成功回调之前,可以在此范围之外更改/覆盖它们
that = this; //should be - 'var that = this;'
因为现在我想不出正确的方法。我会问一个关于正确方法的问题。从技术上讲,这个问题不是关于正确方法的。这就是为什么模型在设置模型时不触发更改事件。虽然我很想知道在主干网中执行此操作的正确方法。显示行:var thisChild=that.children.getsentData.id;thisChild是否未定义?否thisChild未定义。这是正确的模式。