【问题标题】:Creating cards on Trello in a particular order按特定顺序在 Trello 上创建卡片
【发布时间】:2015-05-26 19:48:00
【问题描述】:

我正在使用 Trello client.js 库在 Trello 列表上创建一些卡片,方法是遍历表中的行。但是,按顺序创建卡片很重要。现在,我的代码如下所示:

$("#list > tbody > tr").each(function() {
  $item = "Item: "+$(this).children('td').append(' ').text();
  Trello.post("cards", { name: $item,  idList: "myID"});
});

这可以很好地创建所有卡片;但是,它们并不总是以正确的顺序创建。这并不奇怪,因为 Trello.post 函数是异步的,所以调用是按顺序生成的,但可能在不同的时间内完成。

任何想法如何解决?似乎我需要阻止,直到每次通话结束,但我不太确定如何做到这一点。有什么通用或jquery方法吗?我可以为每个调用指定一个成功函数,所以似乎我也可以向下递归,但不太确定如何执行此操作,尤其是在使用每个函数时。

【问题讨论】:

    标签: javascript jquery trello


    【解决方案1】:

    我可以看到您有两个选择。您可以按顺序进行调用,也可以使用pos 选项来设置顺序。

    串联调用:我建议使用像 async 这样的库,这样就很简单了:

    async.eachSeries($("#list > tbody > tr"), function(item, cb) {
      var title = "Item: "+$(item).children('td').append(' ').text();
      Trello.post("cards", { name: $item,  idList: "myID"}, cb);
    }, function() {
      // Done!  Do anything that needs to happen last here.
    });
    

    pos:卡片按其pos值排序;默认为当前最后一张卡片的 pos + 1024,但您可以设置它以确保正确的顺序:

    var pos = 1024;
    $("#list > tbody > tr").each(function() {
      $item = "Item: "+$(this).children('td').append(' ').text();
      Trello.post("cards", { name: $item,  idList: "myID", pos: pos});
      pos += 1024;
    });
    

    【讨论】:

    • 异步库完全符合我的需要——非常感谢!
    猜你喜欢
    • 2012-09-08
    • 2020-10-20
    • 2014-10-30
    • 2016-04-20
    • 2017-10-07
    • 2013-12-07
    • 2015-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多