筛选集合的更好模式 [英] Better Pattern for Filtering Collections

查看:53
本文介绍了筛选集合的更好模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Games的集合,我希望用户使用复选框对整个集合进行过滤.每次选中/取消选中复选框,都会调用流星订阅以显示相应的Games

I have a collection named Games and I want users to filter through the collection using checkboxes. Every time a checkbox is checked/unchecked, a meteor subscription is called to display the appropriate Games

以下是订阅的外观示例(不正确的mongo代码):

Here's an example of what a subscription might look like (Not proper mongo code):

{region: ['east','west','eu'], skill: ['casual','amatuer','pro'], gamemode: ['ctf','dm','etc']}

在此特定游戏中,您可以按地区,技能和游戏模式进行过滤,每个都有多个组合.

In this particular game you can filter by Region, Skill and Gamemode, with each having multiple combinations.

我需要根据选定的复选框来创建适当的选择器.

What I need is to create the appropriate selector, depending on which checkboxes are checked.

首先,我抓取每个组的已选中复选框,并将其放入数组中,然后将该数组设置为会话变量.

First, I'm grabbing the checked checkboxes - for each group - and throwing them into an array and then setting that array as a session variable.

// Gamemode Group
'click .mode input[type=checkbox]': function(ev, tpl) {
    var mode = tpl.$('.mode input:checked').map(function () {
        return $(this).val();
    });

    var modeArray = $.makeArray(mode);
    Session.set('dotaMode', modeArray);
    returnFilterQuery();
},

然后,我要检查每个分组数组,以查看是否填充了它们,如果有,请将它们添加到最终选择器(query)中,该选择器将传递给订阅.我的问题是,我必须检查每种可能的情况,并且只检查if语句,一旦我开始添加更多的组(游戏模式,区域,技能等)进行搜索,就会有更多的组合. /p>

Then I'm checking each groups array to see if they are populated, and if they are, add them to the final selector (query) which I will pass into the subscription. My problem is I have to check for each possible scenario and its nothing but if statements, and once I start adding more groups (gamemode, region, skill, etc.) to search by, there will be so many more combinations.

function returnFilterQuery(){
    //check all fields and create a query object
    var query = { game: 'dota' };
    var gamemode = Session.get('dotaMode');
    var region = Session.get('dotaRegion');
    var skill = Session.get('dotaSkill');

    // if no region or skill is selected, show user all
    if (region.length && skill.length && gamemode.length) {
        query.region = { $in: region };
        query.skill = { $in: skill };
        query.gamemode = { $in: gamemode };
    } else if (region.length && !skill.length && gamemode.length) {
        query.region = { $in: region };
        query.gamemode = { $in: gamemode };
    } else if (!region.length && skill.length && gamemode.length) {
        query.skill = { $in: skill };
        query.gamemode = { $in: gamemode };
    } else if (!region.length && !skill.length && gamemode.length) {
        query.gamemode = { $in: gamemode };
    } else if (region.length && skill.length && !gamemode.length) {
        query.region = { $in: region };
        query.skill = { $in: skill };
    } else if (region.length && !skill.length && !gamemode.length) {
        query.region = { $in: region };
    } else if (!region.length && skill.length && !gamemode.length) {
        query.skill = { $in: skill };
    }

    return Session.set('dotaFilter', query);
};

希望这一切都有道理.我敢肯定,必须有一种更好的方法,我只是现在无法想到某些事情.谢谢.

Hopefully this all makes sense. I'm sure there has to be a better way, I'm just not able to think of something right now. Thanks.

推荐答案

由于引用了DotA,我对此感到特别受启发. :)

I'm feeling particularly inspired to answer this because of the DotA reference. :)

因此,如果我正确理解了问题,则无需检查所有组合,而应根据三个会话变量的内容向query对象重复添加键.我认为以下内容在逻辑上等同于您上面的returnFilterQuery函数.

So if I understand the problem correctly, you should not need to check every combination, but rather repeatedly add keys to the query object based on the contents of three session variables. I think the following is logically equivalent to your returnFilterQuery function above.

var returnFilterQuery = function() {
  var query = {game: 'dota'};

  var modifyQueryIfArray = function(key, sessionKey) {
    var value = Session.get(sessionKey);
    if (!_.isEmpty(value))
      query[key] = {$in: value};
  };

  modifyQueryIfArray('gamemode', 'dotaMode');
  modifyQueryIfArray('region', 'dotaRegion');
  modifyQueryIfArray('skill', 'dotaSkill');

  return Session.set('dotaFilter', query);
};

请注意,如果会话变量不是数组,则isEmpty可能比length更安全.

Note that isEmpty may be a safer check than length in case the session variable isn't an array.

这篇关于筛选集合的更好模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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