Skip to content

Instantly share code, notes, and snippets.

@Godofbrowser
Forked from gilbert/Entry.js
Created December 25, 2016 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Godofbrowser/b37a4b23b83f2250c21fdb0cf2341586 to your computer and use it in GitHub Desktop.
Save Godofbrowser/b37a4b23b83f2250c21fdb0cf2341586 to your computer and use it in GitHub Desktop.
Event Volunteers: Mithril.js Example from Tutorial Part 1 http://gilbert.ghost.io/mithril-js-tutorial-1/
//
// A model representing an Entry
//
window.Entry = {}
var store = []
var idCounter = 1
Entry.all = function () {
return store
}
Entry.create = function (attrs) {
attrs.id = (idCounter += 1)
attrs.enteredAt = Date.now()
store.push(attrs)
}
Entry.vm = function () {
return {
enteredAt: null,
volunteers: [ Entry.volunteerVM() ]
}
}
Entry.volunteerVM = function () {
return {
name: '[Your name]',
email: '[Your email]'
}
}
//
// A component for entering a new Entry
//
window.EntryForm = {}
EntryForm.controller = function () {
var ctrl = this
ctrl.entry = Entry.vm()
ctrl.add = function () {
ctrl.entry.volunteers.push( Entry.volunteerVM() )
}
ctrl.remove = function (idx) {
ctrl.entry.volunteers.splice(idx, 1)
}
ctrl.submit = function () {
Entry.create( ctrl.entry )
m.route('/')
}
}
EntryForm.view = function (ctrl) {
return m('.entry-form', [
m('h1', "Entry Form"),
m('h3', "Please enter each volunteer's contact information:"),
ctrl.entry.volunteers.map(function(volunteer, idx) {
return m('fieldset', [
m('legend', "Volunteer #" + (idx+1)),
m('label', "Name:"),
m('input[type=text]', {
value: volunteer.name,
onchange: function(e) {
volunteer.name = e.currentTarget.value
}
}),
m('br'),
m('label', "Email:"),
m('input[type=text]', {
value: volunteer.email,
onchange: function(e) {
volunteer.email = e.currentTarget.value
}
}),
removeAnchor(ctrl, idx)
])
}),
m('button', { onclick: ctrl.add }, 'Add another volunteer'),
m('br'),
m('button', { onclick: ctrl.submit }, 'Submit')
])
}
function removeAnchor (ctrl, idx) {
if (ctrl.entry.volunteers.length >= 2) {
return m('button', { onclick: ctrl.remove.papp(idx) }, 'remove')
}
}
//
// A component to list all entries
//
window.EntryList = {}
EntryList.view = function () {
return m('.entry-list', [
m('h1', "All Entries"),
m('a[href=/entries/new]', { config: m.route }, "Add New Entry"),
Entry.all().map( entryView )
])
}
function entryView (entry) {
var date = new Date(entry.enteredAt)
return m('.entry', [
m('label', "Entered at: ", date.toString()),
m('ul', entry.volunteers.map(volunteerView) )
])
}
function volunteerView (volunteer) {
return m('li.volunteer', [
m('label', volunteer.name),
m('label', "(" + volunteer.email + ")")
])
}
// Partially apply arguments to a function. Useful for binding
// specific data to an event handler.
// Example use:
//
// var add = function (x,y) { return x + y; }
// var add5 = add.papp(5)
// add5(7) //=> 11
//
Function.prototype.papp = function () {
var slice = Array.prototype.slice
var fn = this
var args = slice.call(arguments)
return function () {
fn.apply(this, args.concat(slice.call(arguments)))
}
}
<html>
<head>
<title>Volunteer Events</title>
</head>
<body>
<div id="app"></div>
<script src="vendor/mithril.js"></script>
<script src="src/ext/functions.js"></script>
<script src="src/models/Entry.js"></script>
<script src="src/components/EntryForm.js"></script>
<script src="src/components/EntryList.js"></script>
<script>
// Seed some data
Entry.create({
"enteredAt": 1443018758199,
"volunteers": [
{ name: "Alice", email: "alice@example.com" },
{ name: "Bob", email: "bob@example.com" }
]
})
Entry.create({
"enteredAt": 1443019047227,
"volunteers": [
{ name: "Carl", email: "carl@example.com" },
{ name: "Dan", email: "dan@example.com" },
{ name: "Erl", email: "erl@example.com" },
]
})
// Put the component on the page
m.route(document.getElementById('app'), '/', {
'/': EntryList,
'/entries/new': EntryForm
})
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment