Sails.js:订阅用户对请求的特定操作 [英] Sails.js: Subscribing a User to specific actions on a Request
问题描述
在Sails项目中,我有一个User
模型/控制器和一个Request
模型/控制器,以及一个Dashboard
控制器.用户可以使用RequestController.create
请求数据,而管理员可以使用RequestController.grant
批准数据.
In my Sails project, I have a User
model/controller and a Request
model/controller, as well as a Dashboard
controller. A user can make a request for data using RequestController.create
, and an administrator can approve it using RequestController.grant
.
我想要做的是每当一个请求被批准(更新)时通知用户.在RequestController.grant
中,我呼叫Request.publishUpdate(...)
,在DashboardController.display
中,我呼叫
What I want to do is to notify a user whenever one of his/her requests is approved (updated). In RequestController.grant
, I call Request.publishUpdate(...)
, and in my DashboardController.display
, I call
Request.find(req.session.user.id, function(err, requests) {
...
Request.subscribe(req, requests, ['update'])
...
});
然后,在视图/dashboard/display
中,放入<script>
标记:
Then, in the view /dashboard/display
, I put in <script>
tags:
<script>
// Socket handling for notifications
io.socket.on("request", function(obj) {
alert(obj.verb);
alert(obj.data);
alert(obj.previous);
});
</script>
但是,在批准用户的请求并转到仪表板时,不会显示任何警报. sails.io的脚本已经加载,控制台中没有错误.难道我做错了什么?
However, upon approving a user's request and going to the dashboard, no alerts show up. The script for sails.io is already loaded, with no errors in the console. Am I doing something wrong?
推荐答案
此处可能的问题是您在HTTP调用中使用了Request.subscribe
.我假设是这种情况,因为您似乎无法使用DashboardController.display
来显示视图.在这种情况下,Request.subscribe
根本不执行任何操作(它应该真正显示警告),因为它可能无法知道要订阅哪个套接字!
The likely problem here is that you're using Request.subscribe
in an HTTP call. I'm assuming that's the case since it seems like you're problem using DashboardController.display
to display a view. In this case, the Request.subscribe
doesn't do anything at all (it should really display a warning) because it can't possibly know which socket to subscribe!
如果您希望保持控制器逻辑不变(您可能会使用requests
数组作为本地视图来引导页面上的某些数据),那就很好了;快速重构将是测试操作中是否进行了套接字调用:
If you'd like to keep your controller logic the same (you might be using the requests
array as a view local to bootstrap some data on the page), that's fine; a quick refactor would be to test whether or not its a socket call in the action:
// Also note the criteria for the `find` query below--just sending an
// ID won't work for .find()!
Request.find({user: req.session.user.id}, function(err, requests) {
...
// If it's a socket request, subscribe and return success
if (req.isSocket) {
Request.subscribe(req, requests, ['update'])
return res.send(200);
}
// Otherwise continue on displaying the view
else {
...
return res.view('dashboard', {requests: requests});
}
});
然后在视图的某个位置调用io.socket.get('/dashboard/display')
订阅request
实例.
Then somewhere in your view call io.socket.get('/dashboard/display')
to subscribe to request
instances.
如果您将用户ID放在本地,然后将其添加到视图模板中的某个位置,您也可以使用蓝图来订阅来自前端的请求,执行类似io.socket.get('/request?user='+userId)
的操作. <script>var userId=<%=user.id%></script>
.
You could also use blueprints to subscribe to the requests from the front end, doing something like io.socket.get('/request?user='+userId)
, if you put the user ID in the locals and add it somewhere in your view template, e.g. <script>var userId=<%=user.id%></script>
.
这篇关于Sails.js:订阅用户对请求的特定操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!