new approach based on mithril

master
strfry 4 years ago
parent bd1d4dfc12
commit 575d19adbe

@ -0,0 +1,34 @@
var document = require('mithril/test-utils/browserMock')(global).document
const m = require('mithril')
const stream = require('mithril-stream')
// Top Component
function needsModel() {
return {
items: [],
newItem: stream('')
}
}
function NeedsApp() {
const model = needsModel();
return {
view: (vnode) => {
console.log(vnode)
const attrs = vnode.attrs
return m('ul', attrs.items.map(item => m('li', item.name)))
},
oninit: vnode => { console.log('oninit') },
oncreate: vnode => console.log('oncreate'),
onupdate: vnode => console.log('onupdate'),
onbeforeupdate: vnode => console.log('onbeforeupdate')
}
}
//module.exports = { NeedsApp }
module.exports = NeedsApp

@ -0,0 +1,14 @@
{
"dependencies": {
"bulma": "^0.7.4",
"mithril": "^1.1.6",
"mithril-materialized": "^0.11.8",
"mithril-node-render": "^2.3.2",
"mithril-stream": "^2.0.0",
"node-fetch": "^2.5.0",
"polka": "^0.5.2",
"pouchdb": "^7.0.0",
"pouchdb-adapter-http": "^7.0.0",
"pouchdb-core": "^7.0.0"
}
}

@ -0,0 +1,63 @@
var document = require('mithril/test-utils/browserMock')(global).document
const polka = require('polka');
const toHTML = require('mithril-node-render');
var m = require('mithril')
const render = require('mithril-node-render')
var root = document.body
//var x = m.render(root, 'Hello World')
var x = m('body', 'hello world')
var NeedsApp = require('./needs')
var items = [
'Apfel',
'Birne',
'Schokoladenkuchen'
]
function getItems(db_url) {
db_url = "http://exodus.strfry.org:82/hq_needs/"
var db = require('pouchdb')(db_url)
return new Promise((resolve, reject) => {
db.allDocs({include_docs: true, key: 'Supermarkt'}
).then(docs => {
console.log(docs.rows[0].doc.items)
resolve(docs.rows[0].doc.items)
}).catch(reject)
})
}
var template = m('span',
m('h1', 'huhu'),
m(NeedsApp(), {items: items}, 'stuff'))
function render_index(req, res) {
getItems().then(items => {
console.log("the items i found: ", items)
var template = m(NeedsApp(), {items: items})
render(template).then(function (html) {
res.writeHead(200, {'Content-Type': 'text/html'})
res.end(html)
})
})
}
// do a render run for fun
getItems().then(db_docs => {
var template = m(NeedsApp(), {items: db_docs})
render(template).then(html => console.log(html))
})
polka()
.get('/', render_index)
.listen(8000, err => {
if (err) throw err;
console.log(`> Running on localhost:8000`);
});
Loading…
Cancel
Save