1

I'm working on a Derby application and can't seem to figure out how to unsubscribe on page leave. I know how to unsubscribe in general, but hooking the page leave seems to be the problem.

My existing code looks very similar to:

get "/somePath", (page, model) ->
    someQueryHere = model.query("somecollection").someMotif()

    model.subscribe someQueryHere, (err, results) ->
        if err?
            ...
            return

        ...

    do page.render
mjohnson
  • 138
  • 1
  • 9
  • Try posting this to the Google group. Someone there might know the answer https://groups.google.com/forum/?fromgroups=#!forum/derbyjs – switz Oct 09 '12 at 00:40

1 Answers1

1

What do you mean by 'on page leave' exactly? Unsubscribing is not necessary when a full page reload happens, the socket.io connection would be terminated anyway.

On the other hand if you want to unsubscribe from models on a client side page render, you could try using one of the render events:

app.on 'pre:render', ->
  model.unsubscribe someQueryHere

Note, that render events happen after the route have been executed so you should pay attention not to unsubscribe from a query/path you just subscribed to.

Anyway, a more complete example would help to better understand your question.

Another note regarding your example: page.render() should be probably called inside the subscribe callback. Also, why do you use do page.render instead of simply calling it like this: page.render()?

lackac
  • 181
  • 4
  • For do page.render: more of a preference, is there any functional difference? Also, this is primarily to avoid having a bunch of subscriptions that aren't being used (for clients browsing without actually reloading.) Is there any way to do it on a per-page basis to avoid unsubscribing from a query that I just subscribed to? – mjohnson Oct 10 '12 at 07:55
  • There is no functional difference for 'do page.render', although you won't be able to specify arguments. I haven't seen this usage before. I don't know of a way to do what you want at another point. If you explore Derby's source you might find some undocumented event that you could use. The best would be just before route processing begins. Although, on second thought, you could try unsubscribing from everything in a middleware before the app routes, but I don't think that's a good idea. – lackac Oct 10 '12 at 11:58