Skip to content

Instantly share code, notes, and snippets.

@calvinfroedge
Created March 15, 2015 01:41
Show Gist options
  • Save calvinfroedge/5bfbaa9b355a54413a2f to your computer and use it in GitHub Desktop.
Save calvinfroedge/5bfbaa9b355a54413a2f to your computer and use it in GitHub Desktop.
app_navModule = ()->
nav = {}
nav.Link = (data) ->
data.attributes = data.attributes or {}
@href = m.prop(data.href)
@text = m.prop(data.text)
@attributes = m.prop(data.attributes)
return
nav.LinkList = Array
nav.vm = do ->
vm = {}
vm.init = ->
vm.list = new (nav.LinkList)
vm.href = m.prop('')
vm.text = m.prop('')
vm.attributes = m.prop({})
vm.add = ->
if vm.text()
vm.list.push new (nav.Link)(href: vm.href(), text: vm.text(), attributes: vm.attributes())
vm.href ''
vm.text ''
vm.attributes {}
vm
nav.controller = ->
nav.vm
nav.view = ->
m 'nav', [
m('h2', 'test'),
m 'ul', [ nav.vm.list.map((link, index) ->
attributes = link.attributes()
attributes.href = link.href()
m 'li', [
m 'a', attributes, link.text()
]
)]
]
nav.load = ->
m.module document.getElementById(nav.element),
controller: nav.controller
view: nav.view
#initialize the application
{
init: (element)->
if not element
throw new Error 'Element to be bound must be provided'
nav.element = element
nav.vm.init()
nav.load()
add: ()->
addLink = (link, text, attributes)->
nav.vm.href(link)
nav.vm.text(text)
nav.vm.attributes(attributes)
nav.vm.add()
if Array.isArray(arguments[0])
arguments[0].map (item)->
addLink.apply(addLink, item)
else
addLink.apply(addLink, arguments)
nav.load()
}
module = app_navModule()
module.init('header')
module.add [
['foo', 'bar', {
onclick: (event)->
event.preventDefault()
alert('hello')
}]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment