2017-05-17 1 views
0

Meteorクライアントコードは、mongodbドキュメントがその関数にパラメータageを提供しているので、サブスクリプションの準備ができたらopenNewTab(age);関数を呼び出す必要があります。私は試したことの数を失ってしまった。誰がそれを行うことができますか? THXMeteorサブスクリプションが準備されているときに関数を呼び出す方法

//client/main.js 
    Meteor.startup(function() { 
     Tracker.autorun(function() { 
     Meteor.subscribe('myCol', Session.get('age')); 
     }); 
    }); 

//client/lib.js 

Template.footer.events({ 
    'click #submit': (event) => { 
    event.preventDefault(); 
    lib.usageEntry({taskSelected: taskSelected}); 
    } 
}); 
const lib = { 
    'usageEntry': function (Obj) { 
    // do stuff 
    lib[Obj.taskSelected](); // task1 
    }, 
    'task1': function() { 
    let age = document.getElementsByClassName('age')[0].text() 
    Session.set('age', age); // so the reactive subscription works 
    openNewTab(age); //<=== how can this wait till subscription is ready? 
    } 
} 


    //server 
    Meteor.publish('myCol', function (age) { 
     if (!this.userId || Meteor.users.findOne({_id:this.userId}).profile.notOk) return; 
     if (Meteor.users.findOne({_id:this.userId}).profile.hasOwnProperty('notPaid')) return; 
     let matcher = new RegExp('[0-9]{1,3}', "gi"); 
     if (matcher.test(age)) { 
     return myCol.find({age: age}, { 
      fields: { 
      propName: true, 
      }, limit: 1 
     }); 
     } 
    }); 

答えて

2

あなたが実際にサブスクリプションが行われた後に実行されますsubscribe機能へのコールバックを提供することができます。

const HomeStuffCollection = new Mongo.Collection('homeStuff') 

if (Meteor.isClient) { 
    const doSomething = (data) => { /* do stuff */ } 

    Template.home.onCreated(function() { 
    this.subscribe('homeStuff',() => { 
     doSomething(HomeStuffCollection.find().fetch()) 
    }) 
    }) 
} 

if (Meteor.isServer) { 
    Meteor.publish('homeStuff', function() { 
    return HomeStuffCollection.find() 
    }) 
} 

ドキュメント:https://docs.meteor.com/api/pubsub.html#Meteor-subscribe