简体   繁体   中英

Using Card Data in Trello Power-Up

I'm looking for a way to use Trello data in a powerup I am developing. I can get the powerup working as long as I have everything I need during TrelloPowerUp.initialize() , but the data I need is only accessible through Promises. This includes card, board, and powerup data stored through the API. The data Trello gives me access to includes the board, card, plugin IDs, and the command. Since the powerup is expected to return an array, I can't figure out a way to access any other kind of data.

Here is a simple concrete example. In it I would like to check the name of a card and display a badge accordingly.

In the example,
t grants access to the client library
card grants access to the following data:

{
  "context":{
    "board":"55db14fd3e104ac8b105bd75",
    "card":"563b532e4e998440d0d88e62",
    "command":"card-badges",
    "plugin":"564ddf493f184b88ea5ddc0e"
  },
  "locale":"en-US"
} 

Example

Here is the code to initialize card-badges . Notice that the function should return an array.

TrelloPowerUp.initialize({
  'card-badges': function(t, card) {
    var badge_text,
        badge_color;

    // returns a promise with the card name
    t.card('name').then(function (name) {
      if (name == "foo") {
        badge_text = "contingent text";
        badge_color = "contingent color";
      }
    });

    return [{
      text: badge_text,
      icon: './images/icon.png',
      color: badge_color
    }]
  }
});

Obviously this code doesn't work. The initialize function seems to be set up without regard to promises. Even the dynamic option, which takes a function as a parameter, is expected to return an array.

None of this makes sense to me, since storing and retrieving powerup data in cards is also done via promises ( t.set() , t.get() ). Since I don't even seem to be able to access powerup data, I feel like I am missing something in my assessment.

Is there a way to get access to the data available in promises while initializing powerups?

The trick is to return a promise to trello. The promise should return the data Trello is looking for. Eg

TrelloPowerUp.initialize({
  'card-badges': function(t, card) {

    // returns a promise with the card name
    var promise = t.card('name').then(function (name) {
      var badge_text,
          badge_color;
      if (name == "foo") {
        badge_text = "contingent text";
        badge_color = "contingent color";
      }
      return [{
        text: badge_text,
        icon: './images/icon.png',
        color: badge_color
      }]
    });

    return promise;
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM