0

我正在尝试获取 Trello 帐户的成员 ID,然后在构造函数中使用该成员 ID 来生成板。我的问题是我无法访问在我创建的对象之外返回的成员 ID。如何访问 TrllloConnect 对象之外的 memberID?

这是代码:

var TrelloConnect = {
init: function(config) {
    this.config = config;
    this.doAuthorize();
    this.updateLogStatus();
    this.bindLogIn();
    this.bindLogOut();
    this.whenAuthorized();
    this.getMemberID();
},
bindLogIn: function() {
    this.config.connectButton.click(function() {
        Trello.authorize({
            type: "redirect",
            success: this.doAuthorize,
            name: "WonderBoard",
            expiration: "never"
        });
    });
},
bindLogOut: function() {
    this.config.disconnectButton.click(function() {
        var self = TrelloConnect;
        Trello.deauthorize();
        self.updateLogStatus();
    });
},
doAuthorize: function() {
    var self = TrelloConnect;
    self.updateLogStatus();
},
updateLogStatus: function() {
    var isLoggedIn = Trello.authorized();
    this.config.loggedOutContainer.toggle(!isLoggedIn);
    this.config.loggedInContainer.toggle(isLoggedIn);
},
whenAuthorized: function() {
    Trello.authorize({
        interactive: false,
        success: TrelloConnect.doAuthorize
    });
},
getMemberID: function() {
    Trello.members.get("me", function(member) {
        console.log(member.id);
        return member.id;
    });
}
};

 TrelloConnect.init({
    connectButton: $('#connectLink'),
    disconnectButton: $('#disconnect'),
    loggedInContainer: $('#loggedin'),
    loggedOutContainer: $('#loggedout')
});

function Board(memberID) {
    console.log(memberID);
}

var board = new Board(TrelloConnect.getMemberID());
4

1 回答 1

0

Trello.members.get是一个异步函数(即它需要一个回调而不是返回一个值);如果你想对它获取的数据做一些事情,你需要使用回调。

如果你改变getMemberID采取回调

...
getMemberID: function(callback) {
  Trello.members.get("me", function(member){
    callback(member.id);
  });     
}
...

...然后你可以做这样的事情:

TrelloConnect.getMemberId(function(id){
  new Board(id);
});
于 2014-03-27T21:05:23.837 回答