Skip to content

Instantly share code, notes, and snippets.

@fholgado
Created March 19, 2012 18:52
Show Gist options
  • Save fholgado/2123783 to your computer and use it in GitHub Desktop.
Save fholgado/2123783 to your computer and use it in GitHub Desktop.
Load all models before instantiating app
# Let's get all the data first
query_params =
data: {page: 1}
processData: true
new_request: true
App.Email.fetch(query_params)
App.User.fetch()
App.Rule.fetch()
App.PopAccount.fetch()
App.Email.bind 'refresh', @update_email_loading_flag
App.User.bind 'refresh', @update_user_loading_flag
App.Rule.bind 'refresh', @update_rule_loading_flag
App.PopAccount.bind 'refresh', @update_pop_account_loading_flag
update_email_loading_flag: =>
App.cache.loading_flag.email = true
@check_loading_completion()
update_user_loading_flag: =>
App.cache.loading_flag.user = true
@check_loading_completion()
update_rule_loading_flag: =>
App.cache.loading_flag.rule = true
@check_loading_completion()
update_pop_account_loading_flag: =>
App.cache.loading_flag.pop_account = true
@check_loading_completion()
check_loading_completion: =>
if App.cache.loading_flag.rule? and App.cache.loading_flag.user? and App.cache.loading_flag.pop_account? and App.cache.loading_flag.email?
# Let's unbind these initial triggers
App.Email.unbind 'refresh', @update_email_loading_flag
App.User.unbind 'refresh', @update_user_loading_flag
App.Rule.unbind 'refresh', @update_rule_loading_flag
App.PopAccount.unbind 'refresh', @update_pop_account_loading_flag
# Initialize controllers:
@html(@nav = new App.Template)
@append(@root = new App.Root(el: $('#content_container')))
# Let's trigger refresh events so the pages render
App.Email.trigger 'refresh', App.Email.all()
App.User.trigger 'refresh', App.User.first()
App.PopAccount.trigger 'refresh', App.PopAccount.all()
App.Rule.trigger 'refresh', App.Rule.all()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment