Skip to content

Instantly share code, notes, and snippets.

@HendrikRoth
Last active August 29, 2015 14:22
Show Gist options
  • Save HendrikRoth/7c8bbcf4241313e8e410 to your computer and use it in GitHub Desktop.
Save HendrikRoth/7c8bbcf4241313e8e410 to your computer and use it in GitHub Desktop.
var login = {
controller: function(opts, children) {
var backend
opts = opts || {}
backend = opts.backendUrl || 'accounts.yahoo.com'
return {
submit: function() {
m.request(backend, ...)
}
}
},
view: function(ctrl, opts, children) {
return m('.login', [
children,
m('input', ...),
m('input', ...)
])
}
}
var image = {
view: function(ctrl, opts) {
return m('img', {
config: function(element, isInitialized, context) {
if (isInitialized) return
var setSource = function() {
if (element.offsetWidth > 1000) {
element.setAttribute('src', opts.sources[2])
} else if (element.offsetWidth > 600) {
element.setAttribute('src', opts.sources[1])
} else {
element.setAttribute('src', opts.sources[0]])
}
}
setSource()
element.addEventListener('resize', setSource, false)
context.onunload = function() {
element.removeEventListener('resize', setSource)
}
}
})
}
}
//usage:
m.component(login, {backendUrl: 'accounts.google.com'}, [
m('h1', 'Hej, login please.'),
m('h2', 'And discover our features'),
m.component(image, { sources: ['sml.png', 'md.png', 'big.png'] })
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment