Sails.js: Subscribing a User to specific actions on a Request

情到浓时终转凉″ 提交于 2019-12-12 01:58:46

问题


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.

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']) 
    ... 
});

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>

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?


回答1:


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!

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});
    }
});

Then somewhere in your view call io.socket.get('/dashboard/display') to subscribe to request instances.

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



来源:https://stackoverflow.com/questions/25233329/sails-js-subscribing-a-user-to-specific-actions-on-a-request

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