2016-06-09 114 views
0

流星服務器導致集合更新,該更新還更新集合的客戶端副本。
只要集合的客戶端副本中的記錄得到更新,是否有方法來調用客戶端函數?由於流星客戶端mongodb發生變化

//server 
Meteor.users.update({ 
     _id: Meteor.userId() 
    }, { 
     $set: { 
     'profile.goAhead': true 
     }); 

    //client 
    if (Meteor.user().profile.goAhead) { 
     myFunc(); 
    } 
+0

這看起來像一個(近)重複http://stackoverflow.com/questions/29596256/meteor-run-a-function-when-a-mongo-collection-的得到更新 –

回答

0

你想.observeChanges()

let query = MyCollections.find(); 
query.observeChanges({ 
    added(id,fields){ 
    console.log("Document added with _id "+id); 
    }, 
    changed(id,fields){ 
    console.log("Document with _id "+id+" was changed"); 
    }, 
    // ... there are more possible callbacks 
}); 
0

流星有樂觀的更新。客戶端副本首先更新。然後在方便的時候,更新服務器集合。如果服務器更新有任何問題,則客戶端副本將回滾或更新爲不同的值。

在上面的代碼中,服務器更新應該包裝在Meteor.methods()中。從客戶端調用服務器方法有一個回調方法,可以在這種情況下使用。

+0

這裏是一篇樂觀的更新如何工作的文章:https://hashnode.com/post/optimistic-updates-in-meteor-cip7wr11m082zya53uft4xtb5 – vijayst