【问题标题】:Store IDs in a reactive array in Meteor将 ID 存储在 Meteor 的反应式数组中
【发布时间】:2015-10-13 18:17:30
【问题描述】:

我将文档 ID 存储在 Meteor 的响应式字典 (https://manual.meteor.com/#deps-creatingreactivevalues) 中。

最初,我创建字典并将值设置为null

Template.templateName.onCreated(function() {
  this.state = new ReactiveDict;
  this.state.set('selectedId', null);
});

后来我用

设置了ID
this.state.set('selectedId', id);

现在我需要存储多个 ID。我应该将 ID 存储为逗号分隔的字符串值(例如1,5,7,13)还是可以以某种方式存储反应数组?

我怎样才能最好地处理这个问题?如果我将其存储为逗号分隔的字符串,如何在字符串中添加另一个 ID 或测试某个 ID 是否在所选 ID 中?

【问题讨论】:

    标签: javascript arrays node.js meteor


    【解决方案1】:

    使用reactivearrayhttps://atmospherejs.com/manuel/reactivearray

    添加流星包

    meteor add manuel:reactivearray
    

    例子

    var arr = new ReactiveArray(['Tom', 'Dick', 'Harry']);
    
    Template.templateName.helpers({
      names: function() {
        return arr.list();
      }
    });
    

    arr.push('mortensen');
    

    删除

    arr.remove('mortensen');
    

    获取响应式模板助手

    arr.list();
    

    清除列表

    arr.clear();
    

    【讨论】:

      猜你喜欢
      • 2016-05-11
      • 1970-01-01
      • 1970-01-01
      • 2019-01-13
      • 1970-01-01
      • 2020-08-06
      • 2023-03-05
      • 2020-06-09
      • 1970-01-01
      相关资源
      最近更新 更多