规范方法使用Meteor的JQueryUI自动完成 [英] Canonical Way to use JQueryUI Autocomplete with Meteor

查看:86
本文介绍了规范方法使用Meteor的JQueryUI自动完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Meteor,我想了解使用大量服务器端数据的JQuery UI自动完成的最有效方法。

Using Meteor, I'd like to understand the most efficient way to use JQuery UI's Autocomplete with large volumes of server-side data.

我有两个工作提案并希望听到有关差异的意见,以及是否有更好的方法来做同样的事情。

I have two working proposals and would like to hear opinions on the differences and if there are any better ways to do the same thing.

使用pub / sub:

Using pub/sub:

// Server
Meteor.publish("autocompleteData", function (theSearchTerm) {
  var query = {
    name: { $regex: theSearchTerm, $options: 'i'}
  };

  return MyData.find(query, options);
});

// Client
Template.myTemplate.rendered = function() {
  initAutocomplete($(this.find('.my.autocomplete')));
};

var initAutocomplete = function(element){
  element.customAutocomplete({
    source: function(request, callback){
      var sub = Meteor.subscribe('autocompleteData', request.term, function(){
        var results = MyData.find({}, {limit: 50}).fetch();
        sub.stop();
        callback(results);
      });
    },
    select: function(event, ui){
      // Do stuff with selected value
    }
  });
};

使用远程功能(Meteor.Methods):

Using remote functions (Meteor.Methods):

// Server
Meteor.methods({
  getData: function(theSearchTerm) {
    var query = {
      name: { $regex: theSearchTerm, $options: 'i'}
    };

    return MyData.find(query, {limit: 50}).fetch();
  });
});

// Client
Template.myTemplate.rendered = function() {
  initAutocomplete($(this.find('.my.autocomplete')));
};

var initAutocomplete = function(element){
  element.customAutocomplete({
    source: function(request, callback){
      Meteor.call('getData', request.term, function(err, results){
        callback(results);
      });
    },
    select: function(event, ui){
      // Do stuff with selected value
    }
  });
};

如果使用Meteor,使用Meteor设置服务器端自动完成的最有效方式一个大数据集?

Which, if either, is the the most efficient way to setup a server-side autocomplete using Meteor with a large dataset?

推荐答案

为了它的价值,我将提供一些关于这个主题的想法。作为免责声明,我只是一个流星爱好者,而不是专家,所以如果我说错了,请纠正我。

For what it's worth, I'll offer a few of my thoughts on the subject. As a disclaimer, I'm just a Meteor enthusiast and not an expert, so please correct me if I've said something faulty.

对我来说,它似乎是一个在这种情况下,pub / sub的潜在优势是数据被缓存。因此,当订阅相同的记录集时,查找将接近即时,因为客户端可以搜索本地缓存而不是再次向服务器请求数据(发布足够聪明,不会将重复数据推送到客户端)。

To me, it seems like a potential advantage of pub/sub in cases like these is that data is cached. So when subscribing to the same record set, lookup will be near instantaneous since the client can search the local cache instead of asking the server for data again (publication is smart enough not to push repeated data to the client).

但是,由于您停止订阅,因此优势在这里丢失,因此每次用户键入相同的搜索词时,数据都会再次推送到客户端(至少添加了光标)事件再次针对每个文档触发)。在这种情况下,我希望pub / sub与Meteor.call基本相同。

However, the advantage is lost here since you're stopping the subscription, so every time the user types the same search term, data is again pushed to the client (at least, the cursor's added event fires again for every document). In this case I would expect the pub/sub to be on nearly equal footing with Meteor.call.

如果你想缓存pub / sub的数据,单向是取出sub.stop()。但除非您的用户倾向于搜索类似的术语,否则缓存数据实际上更糟糕,因为用户键入的每个字母都会将更多数据存储在客户端上,或许再也看不到了(除非搜索在您的搜索中是如此突出的功能)用户可以从中受益的应用程序?)。

If you want to cache the data of pub/sub, one way is to take out the sub.stop(). But unless your users have the tendency to search similar terms, caching the data is actually worse since with every letter the user types more data will be stored on the client, perhaps never to be seen again (unless searching is such a prominent feature in your app that the user would benefit from this?).

总的来说,我发现使用pub / sub而不是Meteor方法没有令人信服的优势,尽管我不熟悉Meteor足以在两者之间提供更具体的优点/缺点。我个人认为Meteor方法看起来更干净。

Overall, I see no compelling advantage with using pub/sub over Meteor methods, though I'm not versed in Meteor well enough to offer more specific advantages/disadvantages between the two. I personally think Meteor methods looks cleaner though.

如果你想尝试实现搜索功能,我个人喜欢 easy-search 软件包,支持使用自动完成功能的此类服务器端搜索。无论如何,我希望你的问题得到解决!我很想知道答案。

If you're trying to implement a search feature though, I personally like the easy-search package, which supports this type of server-side search with autocomplete. In any case, I hope you get your question resolved! I'm curious to know the answer too.

这篇关于规范方法使用Meteor的JQueryUI自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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