使用SailsJS,有没有一种基于logging中的特定值的Model.watch()方法?

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

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

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

我只想让用户接收他们被允许看到的logging。 我意识到我可以忽略客户端上的那些消息,但是框架仍然会出现在debugging工具中,而客户端忽略不是正确的方法。 是否有一个首选或内置的方式与Sails做到这一点? 从文档和search到,我没有发现任何具体的东西。

当我回到我的电脑时,我会试试这个…

 // 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 

如果这样做比使每个客户订阅单个logging更好的方法,可能使用addRoomMembersToRooms将会很好。 我只是不知道Sails使用/生成的房间名称格式,如Tasklist.subscribe()

谢谢!

我明白了,所以希望这将有助于未来的其他人。

我在节点中使用了console.log(io.sockets.adapter.rooms)来查找房间的名称,命名约定是这样的:(382是一个loggingID)

 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 ,并创build了我自己的groupPublishCreate()函数。

在TasklistController的find函数(不是findOne )的末尾,我订阅了req / socket到我的自定义组空间,而不是使用.watch() ; 这也是一个单线,所以不是太糟糕。

在控制器的create函数中,我嵌套了一个使用sails.sockets.addRoomMembersToRooms的groupPublishCreate函数,将我的自定义房间中的每个人都订阅到已添加的新logging的:update :destroy:message sails.sockets.addRoomMembersToRooms房间。

从这一点开始,当这条logging发生变化时,他们会得到更新,但是他们仍然需要得到一条创build的消息 – 他们不知道还有一条新logging被添加了。 现在,我只是使用Sails使用的相同数据格式将新logging广播到我的自定义房间,所以(对于客户端)它看起来就像正常的Sails“创build的”消息。

在控制器的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) })