Backbone _.each AJAX 依赖项
我目前已经有了这段代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
model.save 返回请求中使用的 xhr 对象。在 jQuery 1.5 中,这些对象是延迟对象,您可以使用它来构建同步机制。
例如,
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,
我没有骨干经验,但我会像这样解决这个问题:
I have no experience with backbone, but I would approach this problem like so:
一种可能的解决方案是创建一个自定义 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.
只需跟踪已处理的事务数量并在最后一个回调中触发销毁,如下所示:
}
Just keep track of the number of transactions already processed and trigger the destroy in the last callback like so:
}