todomvc
Version:
> Helping you select an MV\* framework
67 lines (58 loc) • 2.3 kB
text/coffeescript
ENTER_KEY = 13
window.AppViewModel = ->
#############################
# Shared
#############################
# collections
=
todos: new TodoCollection()
.todos.fetch()
# shared observables
= ko.observable('')
filter_fn = ko.computed(=>
switch
when 'active' then return (model) -> return model.completed()
when 'completed' then return (model) -> return not model.completed()
else return -> return false
)
= kb.collectionObservable(.todos, {view_model: TodoViewModel, filters: filter_fn})
= kb.triggeredObservable(.todos, 'change add remove')
= ko.computed(=> ; return !!.todos.length)
#############################
# Header Section
#############################
= ko.observable('')
= (view_model, event) =>
return true if not $.trim() or (event.keyCode != ENTER_KEY)
# Create task and reset UI
.todos.create({title: $.trim()})
#############################
# Main Section
#############################
= ko.computed(=> ; return .todos.remainingCount())
= ko.computed(=> ; return .todos.completedCount())
= ko.computed(
read: => return not
write: (completed) => .todos.completeAll(completed)
)
#############################
# Footer Section
#############################
= =>
.todos.destroyCompleted()
#############################
# Localization
#############################
=
remaining_message: ko.computed(=> return "<strong>#{@remaining_count()}</strong> #{if @remaining_count() == 1 then 'item' else 'items'} left")
clear_message: ko.computed(=> return if (count = ) then "Clear completed (#{count})" else '')
#############################
# Routing
#############################
router = new Backbone.Router
router.route('', null, => )
router.route('active', null, => )
router.route('completed', null, => )
Backbone.history.start()
return