Rocket Chat Filter随结果集而变化

时间:2017-06-14 21:23:51

标签: meteor rocket.chat

我正在更新火箭聊天应用程序,以便在部门列表页面上有一个部门过滤器。我遇到了一个问题,我的过滤器似乎与结果集绑定到同一个集合。因此,当我更新过滤器时,将删除所有其他过滤器选项。我不确定制作它的最佳方法,因此过滤器只影响结果列表而不影响结果列表。

在: enter image description here

在: enter image description here HTML

<template name="livechatDepartments">
{{#requiresPermission 'view-livechat-manager'}}
<fieldset>
<form class="form-inline" method="post">
    <div class="form-group">
        <label for="department">{{_ "Department"}}</label>
        <select name="department">
            <option value=""></option>
            {{#each departmentsDDL}}
                <option value="{{_id}}">{{name}}</option>
            {{/each}}
        </select>
    </div>

    <div class="form-group">
        <label for="agent">{{_ "Served_By"}}</label>
        <select name="agent">
            <option value=""></option>
            {{#each agents}}
                <option value="{{_id}}">{{username}}</option>
            {{/each}}
        </select>
    </div>
    <button class="button">{{_ "Filter"}}</button>
</form>
</fieldset>
    <div class="list">
        <table>
            <thead>
                <tr>
                    <th width="20%">{{_ "Name"}}</th>
                    <th width="30%">{{_ "Description"}}</th>
                    <th width="10%">{{_ "Num_Agents"}}</th>
                    <th width="10%">{{_ "Num_Available_Agents"}}</th>
                    <th width="20%">{{_ "Enabled"}}</th>
                    <th width="20%">{{_ "Show_on_registration_page"}}</th>
                    <th>{{_ "Delete"}}</th>
                </tr>
            </thead>
            <tbody>
                {{#each departments}}
                    <tr class="department-info row-link" data-id="{{_id}}">
                        <td>{{name}}</td>
                        <td>{{description}}</td>
                        <td>{{numAgents}}</td>
                        <!--<td>{{}}</td>-->
                        <td>{{#if enabled}}{{_ "Yes"}}{{else}}{{_ "No"}}{{/if}}</td>
                        <td>{{#if showOnRegistration}}{{_ "Yes"}}{{else}}{{_ "No"}}{{/if}}</td>
                        <td><a href="#remove" class="remove-department"><i class="icon-trash"></i></a></td>
                    </tr>
                {{/each}}
            </tbody>
        </table>
    </div>
    <div class="text-center">
        <button class="button load-more">{{_ "Load_more"}}</button>
    </div>
    <a href="{{pathFor 'livechat-department-new'}}" class="button primary">{{_ "New_Department"}}</a>
{{/requiresPermission}}

JS:

Template.livechatDepartments.helpers({
  departmentsDDL() {
    return LivechatDepartment.find({}, { sort: { name: -1 } });
  },

  departments() {
    return LivechatDepartment.find({}, { sort: { name: -1 } });
  },

  agents() {
    return AgentUsers.find({}, { sort: { name: 1 } });
  }
});

Template.livechatDepartments.events({
  'click .remove-department' (e /*, instance*/ ) {
    e.preventDefault();
    e.stopPropagation();
    swal({
      title: t('Are_you_sure'),
      type: 'warning',
      showCancelButton: true,
      confirmButtonColor: '#DD6B55',
      confirmButtonText: t('Yes'),
      cancelButtonText: t('Cancel'),
      closeOnConfirm: false,
      html: false
    }, () => {
      Meteor.call('livechat:removeDepartment', this._id, function(error /*, result*/ ) {
        if (error) { return handleError(error); }
        swal({
          title: t('Removed'),
          text: t('Department_removed'),
          type: 'success',
          timer: 1000,
          showConfirmButton: false
        });
      });
    });
  },

  'click .department-info' (e /*, instance*/ ) {
    e.preventDefault();
    FlowRouter.go('livechat-department-edit', { _id: this._id });
  },

  'submit form' (e, instance) {
    e.preventDefault();
    const filter = {};
    $(':input', event.currentTarget)
      .each(function() {
        if (this.name) {
          filter[this.name] = $(this)
            .val();
        }
      });
    instance.filter.set(filter);
    instance.limit.set(20);
  }
});

Template.livechatDepartments.onCreated(function() {
  this.limit = new ReactiveVar(20);
  this.filter = new ReactiveVar({});

  this.subscribe('livechat:agents');

  this.autorun(() => {
    this.subscribe('livechat:departments', this.filter.get(), 0, this.limit.get());
  });
});

流星法:

Meteor.publish("livechat:departments", function(filter = {}, offset = 0, limit = 20) {
      if (!this.userId) {
        return this.error(
          new Meteor.Error("error-not-authorized", "Not authorized", {
            publish: "livechat:agents"
          })
        );
      }

      if (!RocketChat.authz.hasPermission(this.userId, "view-l-room")) {
        return this.error(
          new Meteor.Error("error-not-authorized", "Not authorized", {
            publish: "livechat:agents"
          })
        );
      }

      check(filter, {
        agent: Match.Maybe(String), // agent _id who is serving
        department: Match.Maybe(String)
      });

      const query = {};
      if (filter.agent) {

        const DepartmentFilter = [];
        RocketChat.models.LivechatDepartmentAgents
          .find({
            agentId: filter.agent
          })
          .forEach(department => {
            DepartmentFilter.push(department);
          });

        var depts = DepartmentFilter.map(function(dep) {
          return dep.departmentId;
        });

1 个答案:

答案 0 :(得分:1)

正如您在问题中所述,您的过滤器与结果集绑定在同一个集合中。那么,你怎么能解决这个问题呢?

解决方案1 ​​ - 简单,如果[{"label":"Test Account {12345678901}","category":"Group"},{"label":"* Test Group TEST {1234}","category":"Group"}] 集合中的数据不是太大,可能是最好的:

  • 恢复订阅代码以获取所有数据(未过滤),并过滤livechat:departments辅助函数

    departments
  • 解决方案2 - 使用解决方案1中的过滤器保留// in Template.livechatDepartments.onCreated this.subscribe('livechat:departments'); // in Template.livechatDepartments.helpers departments() { const departmentFilter = Template.instance().filter.get().department; if (departmentFilter){ return LivechatDepartment.find({name: departmentFilter }, { sort: { name: -1 } }); } else { return LivechatDepartment.find({}, { sort: { name: -1 } }); } } 帮助程序, 但现在订阅两次departments

您可以将当前发布重用于已过滤的部门列表(添加过滤后的订阅),并创建一个发布所有部门的新发布/订阅者频道,但只需要发送用于填充的名称+ _id字段选择选项。