@@ -21,6 +21,7 @@ var getOrCreateClient = require('mongodb-scope-client');
2121var ViewSwitcher = require ( 'ampersand-view-switcher' ) ;
2222var View = require ( 'ampersand-view' ) ;
2323var localLinks = require ( 'local-links' ) ;
24+ var async = require ( 'async' ) ;
2425
2526var QueryOptions = require ( './models/query-options' ) ;
2627var Connection = require ( './models/connection' ) ;
@@ -142,17 +143,21 @@ var Application = View.extend({
142143
143144 this . startRouter ( ) ;
144145 } ,
145- startRouter : function ( ) {
146- this . router = new Router ( ) ;
147- debug ( 'Listening for page changes from the router...' ) ;
148- this . listenTo ( this . router , 'page' , this . onPageChange ) ;
149-
150- debug ( 'Starting router...' ) ;
151- this . router . history . start ( {
152- pushState : false ,
153- root : '/'
154- } ) ;
155-
146+ fetchUser : function ( done ) {
147+ debug ( 'preferences fetched, now getting user' ) ;
148+ User . getOrCreate ( this . preferences . currentUserId , function ( err , user ) {
149+ if ( err ) {
150+ metrics . error ( err , 'user: get or create' ) ;
151+ done ( err ) ;
152+ }
153+ this . user . set ( user . serialize ( ) ) ;
154+ this . user . trigger ( 'sync' ) ;
155+ this . preferences . save ( { currentUserId : user . id } ) ;
156+ debug ( 'user fetch successful' , user . serialize ( ) ) ;
157+ done ( null , user ) ;
158+ } . bind ( this ) ) ;
159+ } ,
160+ fetchPreferences : function ( done ) {
156161 // every time the preferences change, test if we should start one of
157162 // the external services.
158163 this . preferences . on ( 'sync' , function ( ) {
@@ -164,22 +169,24 @@ var Application = View.extend({
164169 } . bind ( this ) ) ;
165170
166171 this . preferences . fetch ( {
167- success : function ( ) {
168- User . getOrCreate ( this . preferences . currentUserId , function ( err , user ) {
169- if ( err ) {
170- metrics . error ( err , 'user: get or create' ) ;
171- return ;
172- }
173- this . user . set ( user . serialize ( ) ) ;
174- this . user . trigger ( 'sync' ) ;
175- this . preferences . save ( { currentUserId : user . id } ) ;
176- } . bind ( this ) ) ;
177- } . bind ( this ) ,
172+ success : function ( resp ) {
173+ done ( null , resp ) ;
174+ } ,
178175 error : function ( model , err ) {
179176 metrics . error ( err , 'preferences: fetch' ) ;
180177 }
181178 } ) ;
179+ } ,
180+ startRouter : function ( ) {
181+ this . router = new Router ( ) ;
182+ debug ( 'Listening for page changes from the router...' ) ;
183+ this . listenTo ( this . router , 'page' , this . onPageChange ) ;
182184
185+ debug ( 'Starting router...' ) ;
186+ this . router . history . start ( {
187+ pushState : false ,
188+ root : '/'
189+ } ) ;
183190 app . statusbar . hide ( ) ;
184191 } ,
185192 onFatalError : function ( id , err ) {
@@ -284,40 +291,51 @@ app.extend({
284291 this . trigger ( msg , arg ) ;
285292 } ,
286293 metrics : metrics ,
287- init : function ( ) {
288- domReady ( function ( ) {
289- state . render ( ) ;
290-
291- if ( ! connectionId ) {
292- // Not serving a part of the app which uses the client,
293- // so we can just start everything up now.
294- state . startRouter ( ) ;
295- return ;
296- }
294+ onDomReady : function ( ) {
295+ state . render ( ) ;
296+
297+ if ( ! connectionId ) {
298+ // Not serving a part of the app which uses the client,
299+ // so we can just start everything up now.
300+ state . startRouter ( ) ;
301+ return ;
302+ }
297303
298- app . statusbar . show ( 'Retrieving connection details...' ) ;
304+ app . statusbar . show ( 'Retrieving connection details...' ) ;
299305
300- state . connection = new Connection ( {
301- _id : connectionId
302- } ) ;
306+ state . connection = new Connection ( {
307+ _id : connectionId
308+ } ) ;
303309
304- debug ( 'looking up connection `%s`...' , connectionId ) ;
305- getConnection ( state . connection , function ( err ) {
306- if ( err ) {
307- state . onFatalError ( 'fetch connection' , err ) ;
308- return ;
309- }
310- app . statusbar . show ( 'Connecting to MongoDB...' ) ;
310+ debug ( 'looking up connection `%s`...' , connectionId ) ;
311+ getConnection ( state . connection , function ( err ) {
312+ if ( err ) {
313+ state . onFatalError ( 'fetch connection' , err ) ;
314+ return ;
315+ }
316+ app . statusbar . show ( 'Connecting to MongoDB...' ) ;
311317
312- var endpoint = app . endpoint ;
313- var connection = state . connection . serialize ( ) ;
318+ var endpoint = app . endpoint ;
319+ var connection = state . connection . serialize ( ) ;
314320
315- app . client = getOrCreateClient ( endpoint , connection )
316- . on ( 'readable' , state . onClientReady . bind ( state ) )
317- . on ( 'error' , state . onFatalError . bind ( state , 'create client' ) ) ;
321+ app . client = getOrCreateClient ( endpoint , connection )
322+ . on ( 'readable' , state . onClientReady . bind ( state ) )
323+ . on ( 'error' , state . onFatalError . bind ( state , 'create client' ) ) ;
318324
319- state . startClientStalledTimer ( ) ;
320- } ) ;
325+ state . startClientStalledTimer ( ) ;
326+ } ) ;
327+ } ,
328+ init : function ( ) {
329+ var self = this ;
330+ async . series ( [
331+ state . fetchPreferences . bind ( state ) ,
332+ state . fetchUser . bind ( state )
333+ ] , function ( err ) {
334+ if ( err ) {
335+ throw err ;
336+ }
337+ // as soon as dom is ready, render and set up the rest
338+ domReady ( self . onDomReady ) ;
321339 } ) ;
322340 // set up ipc
323341 ipc . on ( 'message' , this . onMessageReceived . bind ( this ) ) ;
0 commit comments