Skip to content

Instantly share code, notes, and snippets.

@activestylus
Created October 11, 2014 16:51
Show Gist options
  • Save activestylus/f827f8d249545af614e3 to your computer and use it in GitHub Desktop.
Save activestylus/f827f8d249545af614e3 to your computer and use it in GitHub Desktop.
MithrilJS Best Practices
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