我想代码是这样的:
Item.js
var item = {
id = 0,
itemname = "name",
callAjax : function(){
var ajaxdata = {itemname:this.itemname}
MM.callajax("apiname",ajaxdata,function(data){
//this is callback function after ajax is done
id = data; // suppose data = 2
});
},
}
MMCallajax.js
var MM = {
callajax: function (apiname, data, callback) {
var apiurl = this.apiBaseUrl + "/" + apiname;
jQuery.ajax(
apiurl,
{'type': 'POST',
'dataType': 'json',
'data': data,
'xhrFields': { withCredentials: true }
})
.done(this.apiCbDone(callback))
},
apiCbDone : function (callback) {...},
}
我想在使用回调函数
后更新item对象中的id变量如何解决这个问题?
答案 0 :(得分:0)
为什么不喜欢这个?
var item = { id: 0 };
MM.callajax("apiname",ajaxdata,function(data){
//this is callback function after ajax is done
item.id = data; // suppose data = 2
});