1

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!

Suchi
  • 9,989
  • 23
  • 68
  • 112
  • Related? http://stackoverflow.com/questions/4214731/coffeescript-global-variables – Robert Harvey Jan 07 '13 at 19:39
  • 1
    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 07 '13 at 20:05
  • @muistooshort you're right. Edited that. – Suchi Jan 07 '13 at 20:28
  • Why you want to persist your callback? Is it a server-side or browser application? – Renan Ivo Jan 18 '13 at 22:47

1 Answers1

0

I know that the question is old, but I think this can help someone:

// persist while window is open
sessionStorage.myMethod = (function (msg) { alert(msg) }).toString()

// persist forever (while user keeps browser data)
localStorage.myMethod = (function (msg) { alert(msg) }).toString()

Then run the function this way:

// execute from sessionStorage
eval("(" + sessionStorage.myMethod + ")('Hello World!')");

// execute from localStorage
eval("(" + localStorage.myMethod + ")('Hello World!')");
Hiago Takaki
  • 188
  • 2
  • 7