Backbone _.each AJAX 依赖项

发布于 2024-12-19 02:43:12 字数 598 浏览 0 评论 0原文

我目前已经有了这段代码:

handleSubmit: function(e)
{
    var to_bucket = this.$('.transaction_bucket').val();

    // Move all the transactions for this bucket to the selected bucket
    window.app.model.active_transactions.each(
        function(transaction)
        {
            transaction.set({bucket_id: to_bucket});
            transaction.save();
        }
    );

    this.model.destroy({success: function() { window.app.model.buckets.fetch();}});
}

如何修改它,以便只有在所有 _.each ajax 事务发生时才触发销毁?如果我之前有一个 ajax 请求,我只会使用 success: 参数,但我不能在这里这样做。

在骨干中执行此操作的正确方法是什么?

I've got this code currently:

handleSubmit: function(e)
{
    var to_bucket = this.$('.transaction_bucket').val();

    // Move all the transactions for this bucket to the selected bucket
    window.app.model.active_transactions.each(
        function(transaction)
        {
            transaction.set({bucket_id: to_bucket});
            transaction.save();
        }
    );

    this.model.destroy({success: function() { window.app.model.buckets.fetch();}});
}

How can I modify this so that the destroy only triggers once all the _.each ajax transactions happen? If I had one previous ajax request, I would just use the success: parameter, but I can't do that here.

What's the right way to do this in backbone?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

霞映澄塘 2024-12-26 02:43:12

model.save 返回请求中使用的 xhr 对象。在 jQuery 1.5 中,这些对象是延迟对象,您可以使用它来构建同步机制。

例如,

var to_bucket = this.$('.transaction_bucket').val(), 
    calls=[],
    mdestroy=this.model.destroy;

window.app.model.active_transactions.each(function (transaction) {
    transaction.set({bucket_id: to_bucket});
    calls.push(transaction.save());
});

$.when.apply($, calls).then(function () {
    mdestroy({success: function () {window.app.model.buckets.fetch();}});
});

model.save return the xhr object used in the request. With jQuery 1.5, these objects are deferred objects you can use to build a synchronization mechanism.

For example,

var to_bucket = this.$('.transaction_bucket').val(), 
    calls=[],
    mdestroy=this.model.destroy;

window.app.model.active_transactions.each(function (transaction) {
    transaction.set({bucket_id: to_bucket});
    calls.push(transaction.save());
});

$.when.apply($, calls).then(function () {
    mdestroy({success: function () {window.app.model.buckets.fetch();}});
});
流绪微梦 2024-12-26 02:43:12

我没有骨干经验,但我会像这样解决这个问题:

  • 获取 active_transactions 的数量。
  • 在 transaction.save() 上,检查已处理事务的数量(在成功和/或错误回调中),如果它与 active_transactions 的数量匹配,则销毁模型。

I have no experience with backbone, but I would approach this problem like so:

  • Get the number of active_transactions.
  • On transaction.save(), check the number of processed transactions (in the success and/or error callback), if it matches the number of active_transactions, then destroy the model.
我做我的改变 2024-12-26 02:43:12

一种可能的解决方案是创建一个自定义 API 方法,该方法将事务作为参数并在服务器端完成工作。这将减少 https 请求并提高性能。

One possible solution would be to create a custom API method that took the transactions as parameters and did the job on the server side. This would reduce https requests and increase performance as well.

苏大泽ㄣ 2024-12-26 02:43:12

只需跟踪已处理的事务数量并在最后一个回调中触发销毁,如下所示:

handleSubmit: function(e)
{
  var to_bucket = this.$('.transaction_bucket').val();
  var remainingTransactions = window.app.model.active_transactions.length;
  var self = this;

  window.app.model.active_transactions.each(
    function(transaction)
    {
        transaction.save({bucket_id: to_bucket}, {
          success: function(){
            remainingTransactions -= 1;
            if(remainingTransactions < 1) {
              self.model.destroy({success: function() { window.app.model.buckets.fetch();}});
            }
          }
        });
    }
  );

}

Just keep track of the number of transactions already processed and trigger the destroy in the last callback like so:

handleSubmit: function(e)
{
  var to_bucket = this.$('.transaction_bucket').val();
  var remainingTransactions = window.app.model.active_transactions.length;
  var self = this;

  window.app.model.active_transactions.each(
    function(transaction)
    {
        transaction.save({bucket_id: to_bucket}, {
          success: function(){
            remainingTransactions -= 1;
            if(remainingTransactions < 1) {
              self.model.destroy({success: function() { window.app.model.buckets.fetch();}});
            }
          }
        });
    }
  );

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文