I'm trying to get the member ID for a Trello account and then use that member ID in a constructor function to generate boards. My problem is that I can't access the member ID that I return outside of the object I created. How do I access the memberID outside of the TrellloConnect object?

Here is the code:

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());
有帮助吗?

解决方案

Trello.members.get is an asynchronous function (i.e. it takes a callback instead of returning a value); you'll need to use a callback if you want to do something with the data that it fetches.

If you change getMemberID to take a callback

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

... then you could do something like this:

TrelloConnect.getMemberId(function(id){
  new Board(id);
});
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top