Created
May 15, 2015 00:17
-
-
Save pelonpelon/06cfcb1d803e67f3f8cb to your computer and use it in GitHub Desktop.
Mithril: Background AJAX Loading Indicator
This file contains hidden or 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
var LoadingExample = { | |
controller: function () { | |
var ctrl = this | |
// `background: true` is important here. | |
// Otherwise Mithril will wait for the AJAX request to complete before rendering the view. | |
ctrl.users = m.request({ url: 'users', method: 'GET', background: true }) | |
}, | |
view: function (ctrl) { | |
return m('.users', [ | |
m('h3', "All Users"), | |
usersView(ctrl), // Helper method to keep your view clean | |
m('p', "etc. etc.") | |
]) | |
} | |
} | |
function usersView (ctrl) { | |
// If ctrl.users() is empty, then the m.request has not completed yet. | |
if (! ctrl.users()) { | |
// This could be an animated ajax loader | |
return m('p', "Loading...") | |
} | |
return ctrl.users().map(function(user) { | |
return m('.user', user.name) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment