I have code I want to run every time the server is started, which creates an instance of a model. But I keep getting the error:
TypeError: Cannot call method 'build' of undefined
at Object._onTimeout (/home/eric/projects/projects/towerApps/test_towerBot/app/config/shared/application.coffee:8:34)
at Timer.list.ontimeout (timers.js:101:19)
Using "new" has the same result, of course, because the model isn't defined. I know this worked in a slightly older version of tower, but it seems that sometime since the folder structure changed to have stopped working. Is there somewhere else I should put this code? I don't want it to be run based on clients visiting the server, for instance, so putting it in the controller isnt good. I've tried putting it in application.coffee and bootstrap.coffee, and even placing it inside a process.nextTick and various setTimeouts just to see if the issue was with something not loading yet, but it seems that the models just aren't accessible at all from here. Any suggestions?
Yeah this is because the initializers are loaded before the ./app folder:
Two things... The initialization process needs to be more thoroughly thought through (and we should look into the Rails implementation again too), and if you wanted to use some code from a set of code that hasn't been loaded yet (models, controllers, etc., from inside app/config/server/environment or something), there should be a standard way of force-requiring the other code.
Something like that. Let me know what you think. I'll take a deeper look tomorrow.
@viatropos I think, even moreso than a way to force-require other code (though we probably need that too) is a place to put stuff you want to run after everything is loaded. Something like jquery's ready event.
I think having the requirejs stuff @TheHydroImpulse is talking about would be the ideal solution. For a temporary workaround, you can just require the files you need at the top of the script.
i think rails has evented loads so you can hook code to run after something has initialized
self.include_root_in_json = false
@j-mcnally Yeah evented loads or "hooks" would be very useful!
Possibly having a config file to specify which events to hook into and provide a class or function:
@register "pre_controller_load", (event) ->
# Load some class here.. Possibly another library or custom code.
anotherClass = new SomeRandomClassHere()
@register "pre_routing_load", (event) ->
# Do something else here...
@register "pre_shutdown", (event) ->
I wrote a module for this if anyone wants to see if they can add it to tower support or come up with a dsl?
other wise i'll loop back around after some feedback and try to clean it up
@TheHydroImpulse I really like the api you suggested, that looks clean and clear.
@j-mcnally Nice little module, uses some things I haven't even seen in node. The one thing is the initializer should work on both client and server.