Take the 2-minute tour ×

I am trying to register a callback method at the start of my program and then call it at different times throughout the life of my web application. The callback works fine, until I refresh the browser, after which the class variable that I use is reset. What is the right way to go about doing this? Right now I do the following -

class MyManager
    @registerCallback : (callback) ->
        @callback = callback

And its called like -

MyManager.registerCallback myMethod

Any help would be appreciated!

share|improve this question
You don't really have a global, just the "class" variable MyManager.callback. You usually use a cookie (or local-storage) to persist things across page reloads but I can't think of any sane way to store a function that way. I think you need some sort of setup phase attached to an onload handler to do this sort of thing. –  mu is too short Jan 7 at 20:05
@muistooshort you're right. Edited that. –  Suchi Jan 7 at 20:28
Why you want to persist your callback? Is it a server-side or browser application? –  Renan Ivo Jan 18 at 22:47
add comment

1 Answer

From the top of my mind, the only example of a kind of function persistence are in jsonp callbacks.

An example of doing it is the way jQuery does. Consider the example (sorry, no CoffeeScript):

  tags: "mount rainier",
  tagmode: "any",
  format: "json"
function(data) {

When this code is executed, jQuery creates a function that wraps the given callback. The name of this global function has the format jQuery<timestamp>_<nonce>. Note that jQuery uses a nonce, so that it can verify if the function was generated by it. You could do it too, but instead of sending your callback to a remote server, you could serialize it using localStorage or IndexedDB. But you have to remember that storing your nonce in the same place you store your data isn't very clever (Server side applications usually use Sessions or memory to store it).

I don't know your problem in details, but I think you really should consider using Event Emitters instead.

share|improve this answer
add comment

Your Answer


