Skip to content

Instantly share code, notes, and snippets.

@dgkim5360
Last active May 22, 2017 08:38
Show Gist options
  • Save dgkim5360/a20c6ff1cc3520c745706ddf67c5622d to your computer and use it in GitHub Desktop.
Save dgkim5360/a20c6ff1cc3520c745706ddf67c5622d to your computer and use it in GitHub Desktop.
an example mithril code (with bootstrap CSS classes) for the input component of tags
"use strict"
const m = require("mithril")
// Model
const Tags = {
list: [],
add: function(e) {
if (e.keyCode === 13) {
Tags.list.push(e.target.value)
e.target.value = ""
}
},
remove: function(tag) {
const idx = Tags.list.indexOf(tag)
if (idx > -1) Tags.list.splice(idx, 1)
},
}
// View
const TagsInput = {
view: function() {
return m(".container", [
m("input.form-control", {onkeypress: Tags.add}),
m(".tags", Tags.list.map(function(tag) {
return [
m("span.badge.badge-default", [
tag, " ",
m("span", {
onclick: function() {Tags.remove(item)},
}, m.trust("×")),
" ",
])
]
})
])
},
}
m.mount(document.body, TagsInput)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment