Вопрос

In my Meteor app I have Lists which have Items. The Items can belong to multiple Lists, so the Lists contain a field with an array of Item IDs. When I go to a single List view I want a data object with the Items queried from this array of IDs. I think the query would look like this:

Items.find({ _id: { $in:  theArrayOfIds } });

However how/where do I make this query when I load my single List view? At the moment this is my route declaration:

this.route('list', {
  path: '/list/:_id',
  data: function() {
    return {
        list: Lists.findOne(this.params._id)
     }
  }
});

Can I point at the future result of the list object somehow? Or do I make this query somewhere else?

Это было полезно?

Решение

In Iron Router, the data value of your route can be an object as well as a function. See here in the documentation. Therefore you can return both the items and the list in the data call as follows:

this.route('list', {
  path: '/list/:_id',
  data: {
    list: function(){
      var id = Router._currentController.params._id;
      return Lists.findOne(id);
    },
    items: function(){
      var id = Router._currentController.params._id;
      var list = Lists.findOne(id);
      if (!!list){
        /* assuming your array of item ids is a field on list named items */
        var theArrayOfIds = list.items;
      }
      return !!list && Items.find({ _id: {$in: theArrayOfIds}});
    }
});
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top