Commit b7b1e518 authored by Addy Osmani's avatar Addy Osmani

Merge pull request #713 from chaplinjs/chapl

Update Chaplin app
parents 83c41429 5a1a3f33
...@@ -10,12 +10,12 @@ module.exports = class Application extends Chaplin.Application ...@@ -10,12 +10,12 @@ module.exports = class Application extends Chaplin.Application
# Create additional mediator properties # Create additional mediator properties
# ------------------------------------- # -------------------------------------
initMediator: -> initMediator: ->
# Create a user property
mediator.user = null
# Add additional application-specific properties and methods # Add additional application-specific properties and methods
mediator.todos = new Todos() mediator.todos = new Todos()
# If todos are fetched from server, we will need to wait
# for them.
mediator.todos.fetch()
# Seal the mediator # Seal the mediator
super super
start: ->
# If todos are fetched from server, we will need to wait for them.
mediator.todos.fetch()
super
HeaderView = require 'views/header-view' HeaderView = require '../views/header-view'
FooterView = require 'views/footer-view' FooterView = require '../views/footer-view'
TodosView = require 'views/todos-view' TodosView = require '../views/todos-view'
mediator = require 'mediator' mediator = require 'mediator'
module.exports = class IndexController extends Chaplin.Controller module.exports = class IndexController extends Chaplin.Controller
......
...@@ -2,6 +2,7 @@ Application = require 'application' ...@@ -2,6 +2,7 @@ Application = require 'application'
routes = require 'routes' routes = require 'routes'
# Initialize the application on DOM ready event. # Initialize the application on DOM ready event.
$ -> document.addEventListener 'DOMContentLoaded', ->
new Application new Application
controllerSuffix: '-controller', pushState: false, routes: routes controllerSuffix: '-controller', pushState: false, routes: routes
, false
# Application-specific utilities
# ------------------------------
# Delegate to Chaplin’s utils module.
utils = Chaplin.utils.beget Chaplin.utils
Backbone.utils.extend utils,
toggle: (elem, visible) ->
elem.style.display = (if visible then '' else 'none')
# Prevent creating new properties and stuff.
Object.seal? utils
module.exports = utils
View = require './base/view' View = require './base/view'
template = require './templates/footer' utils = require 'lib/utils'
module.exports = class FooterView extends View module.exports = class FooterView extends View
autoRender: true autoRender: true
...@@ -9,7 +9,7 @@ module.exports = class FooterView extends View ...@@ -9,7 +9,7 @@ module.exports = class FooterView extends View
listen: listen:
'todos:filter mediator': 'updateFilterer' 'todos:filter mediator': 'updateFilterer'
'all collection': 'renderCounter' 'all collection': 'renderCounter'
template: template template: require './templates/footer'
render: -> render: ->
super super
...@@ -17,23 +17,24 @@ module.exports = class FooterView extends View ...@@ -17,23 +17,24 @@ module.exports = class FooterView extends View
updateFilterer: (filterer) -> updateFilterer: (filterer) ->
filterer = '' if filterer is 'all' filterer = '' if filterer is 'all'
@$('#filters a') selector = "[href='#/#{filterer}']"
.removeClass('selected') cls = 'selected'
.filter("[href='#/#{filterer}']") @findAll('#filters a').forEach (link) =>
.addClass('selected') link.classList.remove cls
link.classList.add cls if Backbone.utils.matchesSelector link, selector
renderCounter: -> renderCounter: ->
total = @collection.length total = @collection.length
active = @collection.getActive().length active = @collection.getActive().length
completed = @collection.getCompleted().length completed = @collection.getCompleted().length
@$('#todo-count > strong').html active @find('#todo-count > strong').textContent = active
countDescription = (if active is 1 then 'item' else 'items') countDescription = (if active is 1 then 'item' else 'items')
@$('.todo-count-title').text countDescription @find('.todo-count-title').textContent = countDescription
@$('#completed-count').html "(#{completed})" @find('#completed-count').textContent = "(#{completed})"
@$('#clear-completed').toggle(completed > 0) utils.toggle @find('#clear-completed'), completed > 0
@$el.toggle(total > 0) utils.toggle @el, total > 0
clearCompleted: -> clearCompleted: ->
@publishEvent 'todos:clear' @publishEvent 'todos:clear'
View = require './base/view' View = require './base/view'
template = require './templates/header'
module.exports = class HeaderView extends View module.exports = class HeaderView extends View
autoRender: true autoRender: true
el: '#header' el: '#header'
events: events:
'keypress #new-todo': 'createOnEnter' 'keypress #new-todo': 'createOnEnter'
template: template template: require './templates/header'
createOnEnter: (event) => createOnEnter: (event) ->
ENTER_KEY = 13 ENTER_KEY = 13
title = $(event.currentTarget).val().trim() title = event.delegateTarget.value.trim()
return if event.keyCode isnt ENTER_KEY or not title return if event.keyCode isnt ENTER_KEY or not title
@collection.create {title} @collection.create {title}
@$('#new-todo').val '' @find('#new-todo').value = ''
View = require './base/view' View = require './base/view'
template = require './templates/todo'
module.exports = class TodoView extends View module.exports = class TodoView extends View
events: events:
'click .toggle': 'toggle' 'click .toggle': 'toggle'
'dblclick label': 'edit' 'dblclick label': 'edit'
'keypress .edit': 'save' 'keyup .edit': 'save'
'blur .edit': 'save' 'focusout .edit': 'save'
'click .destroy': 'destroy' 'click .destroy': 'clear'
listen: listen:
'change model': 'render' 'change model': 'render'
template: template template: require './templates/todo'
tagName: 'li' tagName: 'li'
destroy: => clear: ->
@model.destroy() @model.destroy()
toggle: => toggle: ->
@model.toggle().save() @model.toggle().save()
edit: => edit: ->
@$el.addClass 'editing' @el.classList.add 'editing'
@$('.edit').focus() @find('.edit').focus()
save: (event) => save: (event) ->
ENTER_KEY = 13 ENTER_KEY = 13
title = $(event.currentTarget).val().trim() title = event.delegateTarget.value.trim()
return @model.destroy() unless title return @model.destroy() unless title
return if event.type is 'keypress' and event.keyCode isnt ENTER_KEY return if event.type is 'keyup' and event.keyCode isnt ENTER_KEY
@model.save {title} @model.save {title}
@$el.removeClass 'editing' @el.classList.remove 'editing'
CollectionView = require './base/collection-view' CollectionView = require './base/collection-view'
template = require './templates/todos'
TodoView = require './todo-view' TodoView = require './todo-view'
utils = require 'lib/utils'
module.exports = class TodosView extends CollectionView module.exports = class TodosView extends CollectionView
container: '#main' container: '#main'
...@@ -11,18 +11,18 @@ module.exports = class TodosView extends CollectionView ...@@ -11,18 +11,18 @@ module.exports = class TodosView extends CollectionView
listen: listen:
'all collection': 'renderCheckbox' 'all collection': 'renderCheckbox'
'todos:clear mediator': 'clear' 'todos:clear mediator': 'clear'
template: template template: require './templates/todos'
render: => render: ->
super super
@renderCheckbox() @renderCheckbox()
renderCheckbox: => renderCheckbox: ->
@$('#toggle-all').prop 'checked', @collection.allAreCompleted() @find('#toggle-all').setAttribute 'checked', @collection.allAreCompleted()
@$el.toggle(@collection.length isnt 0) utils.toggle @el, @collection.length isnt 0
toggleCompleted: (event) => toggleCompleted: (event) ->
isChecked = event.currentTarget.checked isChecked = event.delegateTarget.checked
@collection.each (todo) -> todo.save completed: isChecked @collection.each (todo) -> todo.save completed: isChecked
clear: -> clear: ->
......
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -2,6 +2,9 @@ ...@@ -2,6 +2,9 @@
> Chaplin is an architecture for JavaScript applications using the Backbone.js library. Chaplin addresses Backbone’s limitations by providing a lightweight and flexible structure that features well-proven design patterns and best practices. > Chaplin is an architecture for JavaScript applications using the Backbone.js library. Chaplin addresses Backbone’s limitations by providing a lightweight and flexible structure that features well-proven design patterns and best practices.
In this case, Backbone is replaced with [Exoskeleton](http://exosjs.com),
faster and leaner Backbone without dependencies on jQuery and underscore.
> _[Chaplin - chaplinjs.org](http://chaplinjs.org)_ > _[Chaplin - chaplinjs.org](http://chaplinjs.org)_
......
...@@ -357,19 +357,16 @@ ...@@ -357,19 +357,16 @@
"heading": "Official Resources", "heading": "Official Resources",
"links": [{ "links": [{
"name": "Getting Started", "name": "Getting Started",
"url": "https://github.com/chaplinjs/chaplin/blob/master/docs/getting_started.md" "url": "http://docs.chaplinjs.org/getting_started.html"
}, { }, {
"name": "Documentation", "name": "Documentation",
"url": "https://github.com/chaplinjs/chaplin/tree/master/docs" "url": "http://docs.chaplinjs.org"
}, {
"name": "API Reference",
"url": "https://github.com/chaplinjs/chaplin/tree/master/docs#api-docs"
}, { }, {
"name": "Annotated Source Code", "name": "Annotated Source Code",
"url": "http://chaplinjs.org/annotated/chaplin.html" "url": "http://chaplinjs.org/annotated/chaplin.html"
}, { }, {
"name": "Applications built with Chaplin", "name": "Applications built with Chaplin",
"url": "https://github.com/chaplinjs/chaplin/wiki/Projects-and-companies-using-Chaplin" "url": "http://chaplinjs.org/examples.html"
}, { }, {
"name": "Cookbook", "name": "Cookbook",
"url": "https://github.com/chaplinjs/chaplin/wiki/Cookbook" "url": "https://github.com/chaplinjs/chaplin/wiki/Cookbook"
...@@ -386,6 +383,9 @@ ...@@ -386,6 +383,9 @@
}, { }, {
"heading": "Community", "heading": "Community",
"links": [{ "links": [{
"name": "Support forum on ost.io",
"url": "http://ost.io/@chaplinjs/chaplin"
}, {
"name": "Chaplin on StackOverflow", "name": "Chaplin on StackOverflow",
"url": "http://stackoverflow.com/questions/tagged/chaplinjs" "url": "http://stackoverflow.com/questions/tagged/chaplinjs"
}, { }, {
...@@ -911,7 +911,7 @@ ...@@ -911,7 +911,7 @@
"name": "Exoskeleton", "name": "Exoskeleton",
"description": "A faster and leaner Backbone for your HTML5 apps.", "description": "A faster and leaner Backbone for your HTML5 apps.",
"homepage": "exosjs.com", "homepage": "exosjs.com",
"source_path": [{ "examples": [{
"name": "Architecture Example", "name": "Architecture Example",
"url": "labs/architecture-examples/exoskeleton" "url": "labs/architecture-examples/exoskeleton"
}], }],
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment