1
我采取了非Kosher方法,并使用$.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
});
},
因为目前我无法弄清楚“正确”的方式。我会问一个关于_right_方式的问题。 – undefined
从技术上讲,问题不在于“正确的方式”。这就是为什么模型设置时模型没有触发'change'事件的原因。尽管我很想知道在Backbone中这样做的正确方法。 – Johnston
显示该行的内容:'var thisChild = that.children.get(sentData.id);' 是'thisChild' undefined? –