Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 85 lines (64 sloc) 2.295 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
derby = require 'derby'
{get, view, ready} = derby.createApp module

derby.use(require '../../ui')

view.fn 'noItems',
get: (list) -> !list.length unless list is undefined
set: ->

view.fn 'oneItem',
get: (list) -> list.length == 1 unless list is undefined

# Redirect the visitor to a random todo list
get '/', (page) ->
page.redirect '/' + parseInt(Math.random() * 1e9).toString(36)

# Sets up the model, the reactive function for stats and renders the todo list
get '/:groupName', (page, model, {groupName}) ->
model.query('todos').forGroup(groupName).subscribe ->
model.set '_groupName', groupName

model.ref '_list.all', model.filter('todos')
.where('group').equals(groupName)

model.ref '_list.completed', model.filter('todos')
.where('group').equals(groupName)
.where('completed').equals(true)

model.ref '_list.active', model.filter('todos')
.where('group').equals(groupName)
.where('completed').notEquals(true)

model.set '_filter', 'all'
model.ref '_list.shown', '_list', '_filter'

page.render()

# Transitional route for enabling a filter
get from: '/:groupName', to: '/:groupName/:filterName',
forward: (model, {filterName}) ->
model.set '_filter', filterName
back: (model, params) ->
model.set '_filter', 'all'

get from: '/:groupName/:filterName', to: '/:groupName/:filterName',
forward: (model, {filterName}) ->
model.set '_filter', filterName

ready (model) ->
todos = model.at 'todos'
newTodo = model.at '_newTodo'

exports.add = ->
# Don't add a blank todo
text = newTodo.get().trim()
newTodo.set ''
return unless text
todos.add text: text, group: model.get('_groupName')

exports.del = (e, el) ->
# Derby extends model.at to support creation from DOM nodes
todos.del model.at(el).get('id')

exports.clearCompleted = ->
for {id} in model.get('_list.completed')
todos.del id

exports.clickToggleAll = ->
value = !!model.get('_list.active.length')
for {id} in model.get('_list.all')
todos.set id + '.completed', value

exports.submitEdit = (e, el) ->
el.firstChild.blur()

exports.startEdit = (e, el) ->
item = model.at(el)
item.set '_editing', true

exports.endEdit = (e, el) ->
item = model.at(el)
item.set '_editing', false
if item.get('text').trim() == ''
todos.del item.get('id')
Something went wrong with that request. Please try again.