mortensen
mortensen

Reputation: 1237

Store IDs in a reactive array in Meteor

I store a document ID in a reactive dict (https://manual.meteor.com/#deps-creatingreactivevalues) in Meteor.

Initially, I create the dict and set a value as null:

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

and later I set the ID with

this.state.set('selectedId', id);

Now I need to store multiple IDs. Should I store the IDs with as a comma separated string value (e.g. 1,5,7,13) or is it possible to store reactive arrays somehow?

How can I best handle this? If I store it as a comma separated string, how can I add another ID to the string or test if an ID is among the selected IDs?

Upvotes: 1

Views: 1166

Answers (1)

yasaricli
yasaricli

Reputation: 2533

Use reactivearray package https://atmospherejs.com/manuel/reactivearray

Add meteor package

meteor add manuel:reactivearray

example

var arr = new ReactiveArray(['Tom', 'Dick', 'Harry']);

Template.templateName.helpers({
  names: function() {
    return arr.list();
  }
});

push

arr.push('mortensen');

remove

arr.remove('mortensen');

Get reactive template helper

arr.list();

Clear list

arr.clear();

Upvotes: 3

Related Questions