Search code examples
meteormeteor-blazemeteor-helper

Meteor - Helper comparing two different cursors


I'm using a template helper that returns a comparison between a specific cursor and every iteration of the documents from another cursor. The 'inside' value is stored inside the 'City' collection.

I know that storing a unique 'inside' value on each of the documents inside the 'Places' collection would solve this problem, but you only can be 'inside' one place in the each 'City', this would be a performance issue.

Helpers:

Template.listPlaces.helpers({
  places: function () {
    return Places.find({});
  },
  insidePlace: function () {
    return City.findOne({_id: this._id}).inside === places._id;
  }
]);

Template:

<ul>
  {{#each places}}
    {{#if insidePlace}}Active{{else}}Inactive{{/if}}
  {{/each}}
</ul>

I know a solution would be to have a cursor observer running that would update a Session variable with the 'inside' value each time City.inside gets updated, but I would like to know if there is a better solution.


Solution

  • Have you considered using a transform?

    Template.listPlaces.helpers({
        places: function () {
            var transform = function(doc) {
                var city = Cities.findOne({_id: doc._id});
                doc.insidePlace = (city.inside == doc._id) 
    
                return doc;
            }
            return Places.find({}, {transform: transform});
        },
    ]);