Sails publish(Update) system does not propagate to associations

余生颓废 提交于 2019-12-13 04:42:39

问题


We are building a realtime webapp on sails 0.10.5. We have a lot of models linked through associations. For example we have a User model and a Post model, where a post always has one user.

The populate makes sure that when fetching a user, we also get the posts. We tell the (websocket) client about updates using the subscribe and watch model methods in our sails controllers.

However whenever a websocket client is watching both the User model (watch and subscribed to all users) and the Post model (watch). And a new Post gets created (via another socket for example) our client only receives a new event for Post, while the User changed as well (indirectly through an association).

How can we make sure our clients get those updates as well? Basically we need a way to sync the results of any populated model to our clients.


回答1:


I'm not sure if this is the best way to do that but does the work: Basically, anytime you update a Post you can publish a User "update" event.

// Post.js
module.exports = {

  attributes: {
    title: 'string',
    owner:{
        model:'user'
    }
  },

  afterUpdate: function(post, cb){

    User.publishUpdate(post.owner, {/* props you want to send */});
    cb();

  }
};


来源:https://stackoverflow.com/questions/27254587/sails-publishupdate-system-does-not-propagate-to-associations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!