javascript - Meteor - Return Collection Object from Method -
i'm trying homecoming collection object meteor method based on set session.
here's method call:
template.batches.search = function () { if (typeof session.get('search-parameters') != 'undefined') { var searchparameters = session.get('search-parameters'); homecoming meteor.call('search', searchparameters, function(error , result){ if (error) { console.log(error.reason); } else{ homecoming result; } }); } }
here's method beingness calledl
meteor.methods({ search: function (session) { homecoming batches.find({event: { $regex: session}}); } });
i've read method cannot homecoming collection object , utilize fetch() instead. returns array, however, i'm unable iterate on array colleciton object homecoming results. please advise.
template helpers need synchronous. there few ways solve this, here's example:
template.batches.created = function() { // cleaned when template destroyed this.autorun(function() { var search = session.get('search-parameters'); meteor.subscribe('batchesforsearch', search); }); }; template.batches.helpers({ search: function() { var search = session.get('search-parameters'); homecoming batches.find({event: {$regex: search}}); } });
in batches
template, create autorun subscribes batchesforsearch
whenever session changes. 1 time documents arrive on client, made available template via search
helper.
the batchesforsearch
publish function this:
meteor.publish('batchesforsearch', function(search) { check(search, string); if (_.isempty(search)) { // create sure not publish entire collection if search empty homecoming this.ready(); } else { homecoming batches.find({event: {$regex: search}}); } });
note careful avoid publishing entire collection in case search empty.
javascript meteor
No comments:
Post a Comment