angular-ui / AngularJS-StyleGuide

Example of scalable architecture for my NG-Conf 2014 talk
http://www.youtube.com/watch?v=Iw-3qgG_ipU
284 stars 53 forks source link

How to return query data back to the object that requested it #6

Closed thomasstevens89 closed 9 years ago

thomasstevens89 commented 10 years ago

https://github.com/ProLoser/AngularJS-ORM/blob/master/modules/Objects.coffee#L39-L40

The query() function in BaseObject just pushes the payload to the SocketObject queryStream, which is then responsible for performing the actual query. But how does the data get returned to the original object that requested it?

Is there some sort of callback on the event/payload that allows the socket to return a promise?

While I'm still experimenting with all this, I'm not using sockets myself, instead I just have a dataManager object in place of the socket which will query a REST service.

I know you have that section in the code marked as TODO, but if you could point me in the right direction I'd really appreciate it. Thanks

ProLoser commented 10 years ago

Sorry, I am apparently not getting notifications (or they are going to spam).

Anyway, sockets by nature don't send responses. What we did at our company is tack onto the payload a message id and the backend is then juggling the query message id and appending it to the received payload. In other words, our company 'simulates' a full-circle request. This is not something natively available in sockets. It's up to you wether or not you choose to go down this route or write your application in a completely responsive manner.

In retrospect however, you bring up a very valid point. I didn't think about the fact that using the stream to issue the query doesn't really give you a good way to 'reply' to the original query (lets say if we weren't using sockets to fire the query). Off the top of my head, I don't know a good solution to this. Perhaps the query() function will create a deferred object and pass it up the stream, and when whatever you have that fires the query at the end of the stream gets it's response, it resolves this promise? That seems to be the best solution in my mind.

ProLoser commented 9 years ago

Removed the socket stuff