Skip to content

Instantly share code, notes, and snippets.

@oxalorg
Last active June 15, 2016 20:29
Show Gist options
  • Save oxalorg/0ce21e3dd439c477b233bb8f750788b3 to your computer and use it in GitHub Desktop.
Save oxalorg/0ce21e3dd439c477b233bb8f750788b3 to your computer and use it in GitHub Desktop.
My first simple web app using Mithril.js
// Find this on codepen http://codepen.io/miteshninja/full/gMMrmo/
// Code refactored by @barneycarroll
var app = {};
var api = "https://en.wikipedia.org/w/api.php?format=json&action=query&generator=search&gsrnamespace=0&gsrlimit=10&prop=pageimages|extracts&pilimit=max&exintro&explaintext&exsentences=1&exlimit=max&gsrsearch=";
var cb = '&callback=JSON_CALLBACK';
var wPage = "https://en.wikipedia.org/?curid=";
app.WikiPages = function (title) {
return m.request({
dataType: "jsonp",
url: api + title,
unwrapSuccess: function (response) {
let pageObj = response.query.pages;
let pageList = Object.keys(pageObj).map(key => pageObj[key])
return pageList;
}
});
};
//the controller defines what part of the model is relevant for the current page
//in our case, there's only one view-model that handles everything
app.controller = function() {
var vm = this;
vm.pages = m.prop([])
vm.ss = m.prop('')
vm.getPages = function(){
if (vm.ss()) {
vm.pages = app.WikiPages(vm.ss());
vm.ss("");
}
};
}
app.view = function(ctrl) {
console.log(ctrl);
console.log(ctrl.ss());
return [
m("div.row",
m("div.col-sm-4.col-sm-offset-4",
m("input[type=text].form-control", {
oninput: function(){
ctrl.ss(this.value)
ctrl.getPages()
},
placeholder: "Search something"
})
)
),
m("br"),
m("div.row#pageListRow",
m("div.col-sm-12",
m("ul.pageList",
ctrl.pages().map(function(page){
return m("li.pageListItem",
m("a#white", {
href: wPage + page.pageid,
target: "_blank"
},
page.title
),
m("br"),
m("p", page.extract)
)
})
)
)
)
]
}
m.mount(document.getElementById('app'), app);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment