Callback function in aurelia js

瘦欲@ 提交于 2019-12-12 04:31:09

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!