سؤال

I want to get data from a number of queries on the same collection, and unfortunately this is not yet supported on meteor. That's why I tried to do something like this:

Common

Dep = new Deps.Dependency;

Server

Meteor.methods({
    fetch: function(){
       var results = Data.find(dataQuery).fetch();
       var otherResults = Data.find(queryThatCannotBeCombinedWithPrevious).fetch(); 

       return results.concat(otherResults);
    },
    save: function(data){
         Data.insert(data);
         Dep.changed();
    }
    update: function(data){
         Data.update({_id: data._id}, data);
         Dep.changed();
    }
});

Client

Session.setDefault('combinedData', []);
Template.demo.data = function(){    
    Dep.depend();
    Meteor.call('fetch',function(error, data){
        Session.set('combinedData', data);
    });
    return Session.get('combinedData');
};

This doesn't work though, propably because the Dep variable on the client is different from the Dep on the server. Is there a way to make the method call reactive when the contents of the Data collection change?

Notes

I am currently using Meteor 0.8.1.1, which doesn't allow subscriptions that return multiple cursors of the same collection yet.

هل كانت مفيدة؟

المحلول

This requires a small hack and you're close. First, you need a client–only dependence, the server just passes a data returned from the method and doesn't share variables (also there's nothing on the server that requires deps). Second, you only want to fetch the actual variable once, otherwise you'll end up with an infinite loop.

Example implementation:

var value = null;
var valueInitialized = false;
var valueDep = new Deps.Dependency();

Template.demo.data = function() {
  valueDep.depend();
  if(!valueInitialized) {
    valueInitialized = true;

    Meteor.call('fetchData', function(err, result) {
      value = result;
      valueDep.changed();
    });
  }
  return value;
};
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top