Created
October 11, 2014 16:51
-
-
Save activestylus/f827f8d249545af614e3 to your computer and use it in GitHub Desktop.
MithrilJS Best Practices
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
var app = {}; | |
//model | |
app.PageList = function() { | |
var pages = m.prop([]); | |
var url = "pages.json"; | |
return m.request({method: "GET", url: url}).then(pages); | |
}; | |
// view model | |
app.viewModel = {}; | |
app.viewModel.init = function() { | |
this.pages = app.PageList(); | |
this.rotate = function() { | |
this.pages().push( this.pages().shift() ); | |
}.bind(this); | |
}; | |
//controller | |
app.controller = function() { | |
app.viewModel.init(); | |
}; | |
//view | |
app.view = function(ctrl) { | |
return [ | |
app.viewModel.pages().map(function(page) { | |
return m("a", {href: page.url}, page.title); | |
}), | |
m("a", {onclick: app.viewModel.rotate}, "Rotate links") | |
]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment