Created
August 25, 2014 19:55
-
-
Save legastero/42ef9504e576cba53b2b to your computer and use it in GitHub Desktop.
view-prepare.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// There are cases where we want to go ahead and render a view before | |
// we have data from an API, but we need to know when we're able to | |
// then fetch that data. | |
// | |
// This isn't so much an issue with REST APIs, but this will happen | |
// for any socket.io/WebSocket API where you have to wait for that | |
// connection to be authenticated. | |
// -------------------------------------------------------------------- | |
// Where we listen for new pages in our client init | |
// -------------------------------------------------------------------- | |
app.router.on('newPage', function (page) { | |
mainView.setPage(page); | |
if (page.prepare) { | |
page.listenToAndRun(app, 'change:ready', function () { | |
if (app.ready) { | |
page.prepare(); | |
page.stopListening(app); | |
} | |
}); | |
} | |
}); | |
// -------------------------------------------------------------------- | |
// In our views | |
// -------------------------------------------------------------------- | |
View.extend({ | |
pageTitle: 'Home', | |
template: require('../templates/pages/home.jade'), | |
initialize: function () { | |
// We don't fetch data from the API here, since the API might | |
// not be up and ready to use yet. | |
this.renderWithTemplate(); | |
}, | |
prepare: function () { | |
// This will be called once the app declares that it's ready | |
// for use. | |
app.api.fetchData(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment