Can't create a model from "initializers" #296

Closed
edubkendo opened this Issue Sep 19, 2012 · 10 comments

Projects

None yet

4 participants

@edubkendo
Contributor

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?

Owner

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.

Contributor

@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.

Owner

+1

Owner

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.

Contributor

i think rails has evented loads so you can hook code to run after something has initialized

ala

ActiveSupport.on_load(:active_record) do
  self.include_root_in_json = false
end
Owner

@j-mcnally Yeah evented loads or "hooks" would be very useful!

Owner

Possibly having a config file to specify which events to hook into and provide a class or function:

# app/config/server/events.coffee

Tower.Events.draw ->

    @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) ->
            # Cleanup....
Contributor

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?

https://github.com/j-mcnally/node-obj-watch

other wise i'll loop back around after some feedback and try to clean it up

Contributor
Owner

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment