Skip to content

Instantly share code, notes, and snippets.

@bharathvaj-ganesan
Created March 15, 2017 06:17
Show Gist options
  • Save bharathvaj-ganesan/8e512a85f5387203df689332a9724dd7 to your computer and use it in GitHub Desktop.
Save bharathvaj-ganesan/8e512a85f5387203df689332a9724dd7 to your computer and use it in GitHub Desktop.
Mithril
var users = [
{id: 1, name: "John", email: "[email protected]"},
{id: 2, name: "Mary", email: "[email protected]"},
{id: 3, name: "Bob", email: "[email protected]"}
];
//helper func
var viewModelMap = function(signature) {
var map = {}
return function(key) {
if (!map[key]) {
map[key] = {}
for (var prop in signature) map[key][prop] = m.prop(signature[prop]())
}
return map[key]
}
}
var app = {}
app.controller = function() {
this.users = users;
this.usersVM = viewModelMap({
isEditing: m.prop(false),
tempValue: m.prop(""),
error: m.prop("")
});
};
app.view = function(ctrl) {
return m('div',[
m("a[href=/new]", { config: m.route }, "New app"),
m(newapp,{pctrl : ctrl})
]);
};
var newapp ={};
newapp.controller = function (pattrs) {
/*console.log(attrs);*/
};
newapp.view = function (ctrl,pattrs) {
console.log(pattrs);
console.log(ctrl);
return m('div',[
m("ul", [
pattrs.pctrl.users.map(function(user) {
var vm = pattrs.pctrl.usersVM(user.id);
return m("li", [
m("a[href='javascript:;']", {
onclick: function() {vm.isEditing(!vm.isEditing())}
}, user.name),
vm.isEditing() ? m("div", user.email) : ""
])
})
])
]);
}
m.route(document.body, '/', {
'/': app,
'/new': newapp
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment