问题
Am new to aurelia js. Here, i need to do callback in aurelia js . Here is the code i tried, file.js
this.commonFunctions.get_alrdyShrdUser(this.alrdyshardAry,function(err,result){
if(!err){
console.log("err,result",err,result);
this.sharedAlrdy = result;
}else{
}
});
commonFunctions
get_alrdyShrdUser(docids,callback){
this.httpValueConverter.call_http('sharing/users/list','POST',docids,'test')
.then(data => {
if(data.meta && data.meta.statusCode == 200) {
return callback(null,data.sharedUsers)
}
});
}
Here all works fine, callback function also returned value, but i can't assign value to a aurelia varibale(this.sharedAlrdy).It throws error, Cannot set property 'sharedAlrdy' of undefined. Is that any other way to achieve?
回答1:
This has nothing to do with Aurelia. You are just having a typical JavaScript problem with this
.
Since you are using Aurelia, I assume that is ES6 code and you have arrow function support. Use arrow function in function callbacks and you won't have problems with capturing this
:
this.commonFunctions.get_alrdyShrdUser(this.alrdyshardAry, (err, result) => {
if (!err) {
console.log("err, result", err, result);
this.sharedAlrdy = result;
} else {
}
});
For more details on arrow functions and this
, take a look at MDN.
来源:https://stackoverflow.com/questions/40790615/callback-function-in-aurelia-js