【问题标题】:Using SailsJS, is there a way to Model.watch() based on a certain value in the records?使用 SailsJS,有没有办法 Model.watch() 基于记录中的某个值?
【发布时间】:2017-06-24 21:44:06
【问题描述】:

例如,如果我有一个名为 Tasklist 的模型并执行以下操作:

io.socket.get('/tasklist','',function(data,res){ // do stuff here })

我使用自定义控制器操作,因此客户端将接收列表中的所有当前任务其中 GROUP 字段与用户所属的组匹配。此外,它订阅每条记录,因此它将收到更改通知。但是,如果我使用 Tasklist.watch(req),客户端将收到通知并订阅每条新记录,而不仅仅是与用户组匹配的记录。

我只希望用户接收允许他们查看的记录。我意识到我可以忽略客户端上的那些消息,但是框架仍然会出现在 chrome 调试工具中,并且客户端忽略不是正确的方法。 Sails 是否有首选或内置的方法来执行此操作?从文档和搜索中,我没有找到任何具体的内容。

当我回到我的电脑前我会试试这个......

// for each group user is a member of...
sails.sockets.join(req, 'tasklist:'+group, cb);

// for each POST/CREATE...
sails.sockets.broadcast('tasklist:'+group, 'CREATED', taskID)

// The clients will
io.socket.get('/tasklist/'+taskID)
for the record details and will then be subscribed

如果有比让每个客户订阅单个记录更好的方法,也许使用 addRoomMembersToRooms 会很棒。我只是不知道 Sails 为 Tasklist.subscribe() 之类的东西使用/生成的房间名称格式

谢谢!

【问题讨论】:

    标签: node.js sails.js


    【解决方案1】:

    我想通了,所以希望这会在未来对其他人有所帮助。

    我在node中使用console.log(io.sockets.adapter.rooms)查找房间的名字,命名约定是这样的:(382是记录ID)

    sails_model_tasklist_382:update  // notices about updates to record 382
    sails_model_tasklist_382:destroy // notices about record 382 being deleted
    sails_model_tasklist_382:message // misc msgs you may send about record 382
    
    sails_model_create_tasklist:     // EVERY record addeded to this table/view 
                                     // whether you should get them or not
    

    我放弃了Tasklist.PublishCreate(),转而使用我自己的频道/房间,命名约定类似:sails_model_create_tasklist:group_1,并创建了我自己的groupPublishCreate() 函数。

    在TasklistController 的find 函数(不是findOne)的末尾,我将该req/socket 订阅到我的自定义组房间,而不是使用.watch();它也是单线的,所以还不错。

    在控制器的 create 函数中,我嵌套了一个 groupPublishCreate 函数,该函数使用 sails.sockets.addRoomMembersToRooms 为我的自定义房间中的每个人订阅 :update:destroy:message 房间,以获取添加的新记录。

    从现在开始,当这条记录发生变化时,他们会得到更新,但他们仍然需要得到一条创建的消息——他们还不知道刚刚添加了一条新记录。现在,我只需使用 Sails 使用的相同数据格式将新记录广播到我的自定义房间,因此(对客户而言)它看起来就像普通的 Sails“创建”消息。

    在控制器的 FIND 动作中

    // We could use a loop if result.taskGroup was a collection/array
    // It's a one-liner, no more difficult than Tasklist.watch(req)
    sails.sockets.join(req, 'sails_model_create_tasklist:group_' + result.taskGroup)
    
    // Just to be concise...
    // To disable updates (eg: if a user slides a live-update toggle)
    // This wouldn't actually go here, put it in the right route
    sails.sockets.leave(req, 'sails_model_create_tasklist:group_' + groupIdSentByClient)
    

    在控制器的 CREATE 动作中

    groupPublishCreate('tasklist', result.taskGroup, result)
    
    function groupPublishCreate(modelIdentity, groupID, record) {
      var bcastRoom = 'sails_model_create_' + modelIdentity + ':group_' + groupID
      var subscribeToTheseChannels = [
        'sails_model_' + modelIdentity + '_' + record.id + ':update',
        'sails_model_' + modelIdentity + '_' + record.id + ':destroy',
        'sails_model_' + modelIdentity + '_' + record.id + ':message'
      ]
    
      sails.sockets.addRoomMembersToRooms(
        bcastRoom,
        subscribeToTheseChannels,
        function (err) {
          // error handler goes here
        }
      )
    
      sails.sockets.broadcast(
        bcastRoom,
        modelIdentity,
        {verb: 'created', data: record, id: record.id}
      )
    }
    

    在客户端

    // The incoming frame looks like this (copied from chrome's Network dev tab)
    42["tasklist",{"verb":"created","data":{"msg":"this is a test","id":437},"id":437}]
    // The incoming frame of a Sails PublishCreate() message 
    42["tasklist",{"verb":"created","data":{"msg":"this is a test","id":437},"id":437}]
    // As you can see, they are identical, and can be parsed the same way
    
    io.socket.on('tasklist',function(msg){
      // The CUD is CRUD... R is the Response to a GET
      if (msg.verb === 'created') insertNewTask(msg.data)
      if (msg.verb === 'updated') updateTaskList(msg.data)
      if (msg.verb === 'destroyed') removeTask(msg.id)
    })
    

    不相关,但我这样做是为了将 Sails 用作 jQuery 扩展

    jQuerySails = {
      extend: function () {
        $.API = io.socket
        if ($.API) {
          return ({success: '$.API is available for use'})
        } else {
          return ({error: 'Sails was not extended to $.API'})
        }
      },
      remove: function () {
        delete $.API
        if (!$.API) {
          return ({success: '$.API has been removed'})
        } else {
          return ({error: '$.API is still available for use'})
        }
      }
    }
    
    console.log(jQuerySails.extend())
    
    $.API.on('tasklist',function(msg){
      // The CUD is CRUD... R is the Response to a GET
      if (msg.verb === 'created') insertNewTask(msg.data)
      if (msg.verb === 'updated') updateTaskList(msg.data)
      if (msg.verb === 'destroyed') removeTask(msg.id)
    })
    

    【讨论】:

      猜你喜欢
      • 2020-06-14
      • 2017-12-24
      • 2021-12-13
      • 2023-02-14
      • 2021-08-26
      • 1970-01-01
      • 1970-01-01
      • 2018-11-14
      • 2012-02-01
      相关资源
      最近更新 更多