【问题标题】:Meteor client mongodb on changeMeteor 客户端 mongodb 更改
【发布时间】:2016-06-09 13:52:27
【问题描述】:

Meteor 服务器会导致集合更新,这也会更新集合的客户端副本。
每当集合的客户端副本中的记录更新时,有没有办法调用客户端函数?谢谢

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

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

【问题讨论】:

标签: meteor


【解决方案1】:

你想要.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
});

【讨论】:

    【解决方案2】:

    Meteor 有乐观更新。首先更新客户端副本。然后在方便的时候更新服务器集合。如果服务器更新有任何问题,则客户端副本将回滚或使用不同的值更新。

    在上面的代码中,服务器更新应该包装在 Meteor.methods() 中。从客户端调用服务器方法有一个回调方法,可以在这种情况下使用。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-18
    • 1970-01-01
    • 2015-10-15
    • 2014-03-28
    • 1970-01-01
    • 1970-01-01
    • 2018-01-14
    • 2016-06-08
    相关资源
    最近更新 更多