I'm looking for a way to "unbind" jQuery's $.when()
after the .done()
callback has been executed a single time, analogous to using jQuery's .one()
instead of .on()
for a single event. The reason why I am using deferred objects instead of events in this circumstance is because I need to wait for multiple asynchronous processes to complete before the callback can be executed.
To go into more detail, I'm working on a backbone application where I have a model and view for each page. The sequence of displaying a new page looks something like this:
- view for the incoming page (call it page A) is told to render
$.when()
is used by page A to wait for asynchronous processes (3, 4 & 5) to resolve- data for page A is loaded from the server, populating the model
- images in page A's model are preloaded
- the previous page, page B, finishes closing (animation is complete)
- page A is rendered
This works fine in a hypothetical scenario where each page is only visited once by a user. However, pages are obviously subject to repeat visits, which means that when page B is opened and closed again after the user navigates elsewhere on the website, the deferred object related to the completion of it's .close()
method is resolved once more, executing the code block of the .done()
callback in page A's .render()
method, which is not good.
Ideally, the $.when().done()
code block would be "unbound" as soon as it is executed once, or in the .close()
method of each page, just as many other event listeners are unbound.
I'm not sure if this is even possible, so maybe there as alternative method to achieve what I'm trying to do here? Maybe their is a path that ditches deferreds and listens for multiple events to fire?
Thanks.