我试图在模型内部的 ajax 调用中为模型设置一些属性,但它不起作用。我在 ajax 调用的成功函数中使用了 this.set 但不起作用。我不知道这个的确切方法。
var Person = Backbone.Model.extend({
url:"https://api.parse.com/1/classes/_User/", defaults:{},
initialize:function(){
var self=this.persona;
console.log("inperson");
this.upload();
},
validate:function(){
console.log("validate");
},
upload:function(){
var serverUrl = 'https://api.parse.com/1/files/' + file.name;
$.ajax({
type: "POST",
beforeSend: function(request) {
request.setRequestHeader("X-Parse-Application-Id", 'qS0KLMx**1tyhM9EEPiTS3VMk');
request.setRequestHeader("X-Parse-REST-API-Key", 'nh3eoUo9*s9VQzvbF2gMhcKJIfIt1Gm');
request.setRequestHeader("Content-Type", file.type);
},
url: serverUrl,
data: file,
processData: false,
contentType: false,
success: function(data) {
console.log("File available at: " + data.name);
this.set({image: {"name" :data.name, "__type" : "File"}});//THE PROBLEM IS HERE!!
},
error: function(data) {
var obj = jQuery.parseJSON(data);
alert(obj.error);
}
});
}
});
return Person;
});