2016-05-06 5 views
0

投票者の総数を計算するためのカーソルが1つあります。私は、最初に自分の好意で有権者の総数を数え、2番目の方法は投票した有権者の総数を数える2つの方法を持っています。MeteorjsのCollection.observe()イベントに対する複数のメソッド呼び出し

LIB/collection.js

infavourcount = new Mongo.Collection('infavourcount'); 

votedone = new Mongo.Collection('votedone'); 

サーバー/ publish.js [私の賛成で有権者をカウント]

function upsertInFavourCount() { 
    var yes = voters.find({favour: {$regex:"Y", $options: 'i'}}).count(); 
    var maybe = voters.find({favour: {$regex:"M", $options: 'i'}}).count(); 
    var no = total - (yes + maybe); 
    infavourcount.upsert('infavour', 
    { 
     yes: yes , 
     maybe: maybe, 
     no:no 
    } 
); 
} 

// null name means send to all clients 
Meteor.publish(null,function() { 
    upsertInFavourCount(); 
    return infavourcount.find(); 
}); 

サーバー/ publish.js [成功votingsをカウント]

function upsertVoteDone() { 
    var done = voters.find({voted: {$regex:"Y", $options: 'i'}}).count(); 
    votedone.upsert('votedone', 
     { 
     done: done 
     } 
    ); 
} 

Meteor.publish(null,function() { 
    upsertVoteDone(); 
    return votedone.find(); 
}); 

var cursor = voters.find(); 

cursor.observe({ 
    changed: upsertVoteDone 
}); 

クライアント/鋳型/ home.js

Template.home.onCreated(function(){ 
    Meteor.subscribe('voters'); 
    Meteor.subscribe('infavourcount'); 
    Meteor.subscribe('votedone'); 
}); 

Template.home.helpers({ 
    yesvote : function() { 
    return infavourcount.findOne().yes; 
    }, 
    maybevote : function() { 
    return infavourcount.findOne().maybe; 
    }, 
    novote : function() { 
    return infavourcount.findOne().no; 
    }, 
    votedone : function() { 
     return votedone.findOne().done; 
    } 

})。

私の問題は、Meteorの公開コレクションの1つの観測方法で複数のupsertメソッドを呼び出す方法です。

答えて

0

私は自分の解決策を得ました。これに対する答えは、 "変更された"または "追加された"または "削除された"イベントに関数を追加し、n個のupsertメソッドを呼び出します。

cursor.observe({ 
    changed: function(id, fields){ 
    upsertInFavourCount(); 
    upsertVoteDone(); 
} 

});

関連する問題