How to catch the event of a linked collection having an UPDATE via autosubscribe pub/sub?

35 views
Skip to first unread message

M. Brett Ansite

unread,
Feb 18, 2015, 12:01:09 AM2/18/15
to sai...@googlegroups.com
Hello!
I have scoured the internet madly for this and these are the posts that seem to relate to what I'm trying to do the most:

but they don't quite touch on it.

I have 
Model Rooms
autosubscribe:['add:people','update:people']
attributes
:{
     people
:{collection:'people',via:'room'},
     temp
:{type:'integer'}
},


Model People
 attributes: {
      name
:{type:'string'},
      room
:{model:'rooms'},
      status
:{type:'string',enum:['frustratedWithOfficialDocumentationBeingSoFracturedBetween:HiddenGitHubs_GoogleMisdirectsOnWebsite_OldYoutubes_ConceptsReferenceWhichAreForNoReasonSplitUp','personIsDead']
 
},


Now, lets say that instead of adding another person in a room (which could fire the publishAdd event) I have found that one of my people has died and I need to simply update their status

People.findOne({name:'mrHappy'}).exec(err,mrHappyObj){
    mrHappyObj
.status = 'personIsDead'
    mrHappyObj.save()  //etc
    People.publishUpdate(mrHappyObj.id,{status:mrHappyObj.status})
})

So this is great for everyone subscribed to 'mrHappy' but it would be so awesome if I could find the thing that would tell the ROOM he was associated with that he was dead automatically, I don't care if whatever it is only gives me mrHappy's id, I would really like to get notified automatically. 






bonus stuff that I tried but not necessary to read.
I wrote this function in my People model but it seems very kludgy

 afterUpdate: function(updatedRecord, next)
 
{
 sails
.log.debug("I updated a People! derp",updatedRecord);
 sails
.log.debug("key is ",sails.models[this.identity].primaryKey);
       
var pKey = sails.models[this.identity].primaryKey
 
var thisModelId = this.identity
        _
.each(Z.getAssociationParents(this.identity), function(association) {
   
// //so we now have the name of a parent model, we now have to find the id() of the parent that this
   
// //NEW thing is pointing to?
   
// //This is the parent that needs to be notified that he now owns a new vehicle. We have to take
   
// //the socket in his room and then use it to subscribe to this change!
//                 console.log("parent model found:",association)
                sails
.log.debug("parent room",'sails_model_'+association+'s_'+ updatedRecord[association]+':'+'update')
               
var sockets = sails.sockets.subscribers('sails_model_'+association+'s_'+ updatedRecord[association]+':'+'update')


                sails
.log.debug("child room",'sails_model_'+thisModelId+'_'+ updatedRecord[pKey] +':'+'update')
               
var deleteMeSocketsInChild = sails.sockets.subscribers('sails_model_'+thisModelId+'_'+ updatedRecord[pKey] +':'+'update')
               
                sails
.log.debug("sockets in parent:", sockets, "child:",deleteMeSocketsInChild)
               
               
for(var s in sockets)
               
{
                   
var sock = sails.io.sockets.socket(sockets[s]);
 
 
//TODO !! send the subscribe method the needed primary key object
                    sails
.models[thisModelId].subscribe(sock, Z.subscribePluralizer(pKey,updatedRecord[pKey])); //TODO - get the primary key as the last paramater to this function (updaterd record
 sails
.log.debug("break")
   
//   //could pass it a null


   
// //If i am correct, updatedRecord is the whole record that has been updated. We are wanting to
   
// //subscribe the parent socket(s) to it. If this doesn't work , try using the information within
   
// //the udpatedRecord to do the subscribe unless you can think of a better way.
 sails
.log.debug("sockets in parent:", sockets, " NEW child:",deleteMeSocketsInChild)
               
}
               
       
});
 
next()
 
}








the other functions
 //return the models that are your parents
 getAssociationParents
: function(modelName) {
 
var assocArr = []
 
if (sails.models[modelName]) {
 
for (var a in sails.models[modelName].attributes) {
 
if (sails.models[modelName].attributes[a].model)
 assocArr
.push(a)
 
}
 
}
 
return assocArr
 
},


 //inspired by the pluralize function in \sails\lib\hooks\pubsub\index.js - we have to wrap our primary key up all pretty for it
 
// since we don't expect our updatedRecords function to return an array of new objects (it shouldn't) we won't use the _.map function from pluralize
 subscribePluralizer
: function(pKey, value) {
 
//this function should be examined before using - 1-28-2015
 
var newObj = {}
 newObj
[pKey] = value
 newObj
= [newObj]


 
return newObj
 
},






M. Brett Ansite

unread,
Feb 18, 2015, 11:25:29 PM2/18/15
to sai...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages