Meteor - 从客户端取消服务器方法 [英] Meteor - Cancelling a Server Method from the Client

查看:111
本文介绍了Meteor - 从客户端取消服务器方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过服务器方法执行数据库计数。用户可以选择他们想要执行计数的方式,然后调用该方法。

I'm performing a database count through a server method. Users can select how they want the count to be performed and then invoke the method.

我的问题是计数可能需要一些时间,用户可能会改变主意该方法正在运行并请求不同的计数。有没有办法让我取消调用的方法并运行一个新计数?

My problem is that the count can take some time and a user might change their mind while the method is running and request a different count. Is there any way for me to cancel the invoked method and run a new count?

我认为this.unblock()可能有用;它将允许运行一个新方法,但它不会取消旧方法。我也考虑过预先计数然后只使用查找,但选择器组合太多了。

I've thought this.unblock() might work; it will allow a new method to be run, but it won't cancel the old method. I've also considered pre-counting and then just using a lookup, but there are too many selector combinations.

这是我的代码,它很简单:

Here's my code, it's fairly simple:

//Server
Meteor.methods({
    getFilterCount: function(oFilterSelector) {
        return clMaBldgs.find(oFilterSelector, {}).count();
    }
});

//Client
Meteor.call('getFilterCount', oFilterSelector, function (error, result) {
    //do some stuff
});


推荐答案

从客户端取消已经对服务器进行的方法调用不可能。

Cancelling an already-made method call to server from client is not possible.

但是如果你的方法在短时间内被多次调用,你只关心最后一次调用的结果。您可以使用去抖动来延迟执行一段时间。这有助于减少对服务器的不必要调用。

But if your method are called multiple times in a short interval, and you only care about the last call's result. You could use debounce to delay execution for some times. This will help reduce unnecessary calls to server.

这篇关于Meteor - 从客户端取消服务器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆