Commit 41470676 authored by Paul Miller's avatar Paul Miller

Chaplin-brunch: do real model-level filtering, get rid of stuff.

parent 466147a1
mediator = require 'mediator' mediator = require 'mediator'
Todos = require 'models/todos' Todos = require 'models/todos'
Layout = require 'views/layout'
# The application object # The application object
module.exports = class Application extends Chaplin.Application module.exports = class Application extends Chaplin.Application
...@@ -8,13 +7,6 @@ module.exports = class Application extends Chaplin.Application ...@@ -8,13 +7,6 @@ module.exports = class Application extends Chaplin.Application
# “Controller title – Site title” (see Layout#adjustTitle) # “Controller title – Site title” (see Layout#adjustTitle)
title: 'Chaplin • TodoMVC' title: 'Chaplin • TodoMVC'
# Override standard layout initializer
# ------------------------------------
initLayout: ->
# Use an application-specific Layout class. Currently this adds
# no features to the standard Chaplin Layout, it’s an empty placeholder.
@layout = new Layout {@title}
# Create additional mediator properties # Create additional mediator properties
# ------------------------------------- # -------------------------------------
initMediator: -> initMediator: ->
...@@ -22,6 +14,8 @@ module.exports = class Application extends Chaplin.Application ...@@ -22,6 +14,8 @@ module.exports = class Application extends Chaplin.Application
mediator.user = null 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() mediator.todos.fetch()
# Seal the mediator # Seal the mediator
super super
...@@ -9,7 +9,6 @@ ...@@ -9,7 +9,6 @@
<!--[if IE]> <!--[if IE]>
<script src="../../../assets/ie.js"></script> <script src="../../../assets/ie.js"></script>
<![endif]--> <![endif]-->
<link rel="stylesheet" href="app.css">
<script src="app.js"></script> <script src="app.js"></script>
<script>require('initialize');</script> <script>require('initialize');</script>
</head> </head>
......
HeaderView = require 'views/header-view'
FooterView = require 'views/footer-view'
TodosView = require 'views/todos-view'
mediator = require 'mediator'
module.exports = class Controller extends Chaplin.Controller
beforeAction: ->
@compose 'footer', ->
params = collection: mediator.todos
@header = new HeaderView params
@footer = new FooterView params
@todos = new TodosView params
Controller = require './base/controller' HeaderView = require 'views/header-view'
FooterView = require 'views/footer-view'
TodosView = require 'views/todos-view'
mediator = require 'mediator'
module.exports = class IndexController extends Controller module.exports = class IndexController extends Chaplin.Controller
# The method is executed before any controller actions.
# We compose structure in order for it to be rendered only once.
beforeAction: ->
@compose 'structure', ->
params = collection: mediator.todos
@header = new HeaderView params
@footer = new FooterView params
# On each new load, old @view will be disposed and
# new @view will be created. This is idiomatic Chaplin memory management:
# one controller per screen.
list: (params) -> list: (params) ->
@publishEvent 'todos:filter', params.filterer?.trim() ? 'all' filterer = params.filterer?.trim() ? 'all'
@publishEvent 'todos:filter', filterer
@view = new TodosView collection: mediator.todos, filterer: (model) ->
switch filterer
when 'completed' then model.get('completed')
when 'active' then not model.get('completed')
else true
...@@ -4,4 +4,4 @@ routes = require 'routes' ...@@ -4,4 +4,4 @@ routes = require 'routes'
# Initialize the application on DOM ready event. # Initialize the application on DOM ready event.
$ -> $ ->
new Application new Application
controllerSuffix: '-controller', pushState: no, routes: routes controllerSuffix: '-controller', pushState: false, routes: routes
# Application-specific utilities
# ------------------------------
# Delegate to Chaplin’s utils module
utils = Chaplin.utils.beget Chaplin.utils
# _(utils).extend
# someMethod: ->
module.exports = utils
mediator = require 'mediator'
utils = require './utils'
# Application-specific view helpers
# ---------------------------------
# http://handlebarsjs.com/#helpers
# Conditional evaluation
# ----------------------
# Choose block by user login status
Handlebars.registerHelper 'if_logged_in', (options) ->
if mediator.user
options.fn(this)
else
options.inverse(this)
# Map helpers
# -----------
# Make 'with' behave a little more mustachey
Handlebars.registerHelper 'with', (context, options) ->
if not context or Handlebars.Utils.isEmpty context
options.inverse(this)
else
options.fn(context)
# Inverse for 'with'
Handlebars.registerHelper 'without', (context, options) ->
inverse = options.inverse
options.inverse = options.fn
options.fn = inverse
Handlebars.helpers.with.call(this, context, options)
# Evaluate block with context being current user
Handlebars.registerHelper 'with_user', (options) ->
context = mediator.user?.serialize() or {}
Handlebars.helpers.with.call(this, context, options)
Model = require 'models/base/model'
module.exports = class Collection extends Chaplin.Collection
# Use the project base model per default, not Chaplin.Model
model: Model
# Mixin a synchronization state machine
# _(@prototype).extend Chaplin.SyncMachine
module.exports = class Model extends Chaplin.Model
# Mixin a synchronization state machine
# _(@prototype).extend Chaplin.SyncMachine
Model = require 'models/base/model' # It is a very good idea to have base Model / Collection
# e.g. Model = require 'models/base/model'
module.exports = class Todo extends Model # But in this particular app since we only have one
# model type, we will inherit directly from Chaplin Model.
module.exports = class Todo extends Chaplin.Model
defaults: defaults:
title: '' title: ''
completed: no completed: no
......
Collection = require 'models/base/collection'
Todo = require 'models/todo' Todo = require 'models/todo'
module.exports = class Todos extends Collection module.exports = class Todos extends Chaplin.Collection
model: Todo model: Todo
localStorage: new Store 'todos-chaplin' localStorage: new Store 'todos-chaplin'
......
...@@ -4,3 +4,4 @@ module.exports = class CollectionView extends Chaplin.CollectionView ...@@ -4,3 +4,4 @@ module.exports = class CollectionView extends Chaplin.CollectionView
# This class doesn’t inherit from the application-specific View class, # This class doesn’t inherit from the application-specific View class,
# so we need to borrow the method from the View prototype: # so we need to borrow the method from the View prototype:
getTemplateFunction: View::getTemplateFunction getTemplateFunction: View::getTemplateFunction
useCssAnimation: true
require 'lib/view-helper' # Just load the view helpers, no return value
module.exports = class View extends Chaplin.View module.exports = class View extends Chaplin.View
# Precompiled templates function initializer. # Precompiled templates function initializer.
getTemplateFunction: -> getTemplateFunction: ->
......
View = require 'views/base/view' View = require './base/view'
template = require 'views/templates/footer' template = require './templates/footer'
module.exports = class FooterView extends View module.exports = class FooterView extends View
autoRender: yes autoRender: true
el: '#footer' el: '#footer'
events: events:
'click #clear-completed': 'clearCompleted' 'click #clear-completed': 'clearCompleted'
...@@ -16,7 +16,6 @@ module.exports = class FooterView extends View ...@@ -16,7 +16,6 @@ module.exports = class FooterView extends View
@renderCounter() @renderCounter()
updateFilterer: (filterer) -> updateFilterer: (filterer) ->
console.log 'updateFilterer'
filterer = '' if filterer is 'all' filterer = '' if filterer is 'all'
@$('#filters a') @$('#filters a')
.removeClass('selected') .removeClass('selected')
......
View = require 'views/base/view' View = require './base/view'
template = require 'views/templates/header' template = require './templates/header'
module.exports = class HeaderView extends View module.exports = class HeaderView extends View
autoRender: yes autoRender: true
el: '#header' el: '#header'
events: events:
'keypress #new-todo': 'createOnEnter' 'keypress #new-todo': 'createOnEnter'
......
# Layout is the top-level application ‘view’.
module.exports = class Layout extends Chaplin.Layout
listen:
'todos:filter mediator': 'changeFilterer'
changeFilterer: (filterer = 'all') ->
$('#todoapp').attr 'class', "filter-#{filterer}"
View = require 'views/base/view' View = require './base/view'
template = require 'views/templates/todo' template = require './templates/todo'
module.exports = class TodoView extends View module.exports = class TodoView extends View
events: events:
...@@ -15,12 +15,6 @@ module.exports = class TodoView extends View ...@@ -15,12 +15,6 @@ module.exports = class TodoView extends View
template: template template: template
tagName: 'li' tagName: 'li'
render: =>
super
# Reset classes, re-add the appropriate ones.
@$el.removeClass 'active completed'
@$el.toggle @model.get('completed')
destroy: => destroy: =>
@model.destroy() @model.destroy()
......
CollectionView = require 'views/base/collection-view' CollectionView = require './base/collection-view'
template = require 'views/templates/todos' template = require './templates/todos'
TodoView = require 'views/todo-view' TodoView = require './todo-view'
module.exports = class TodosView extends CollectionView module.exports = class TodosView extends CollectionView
el: '#main' container: '#main'
events: events:
'click #toggle-all': 'toggleCompleted' 'click #toggle-all': 'toggleCompleted'
itemView: TodoView itemView: TodoView
......
...@@ -9,7 +9,6 @@ ...@@ -9,7 +9,6 @@
<!--[if IE]> <!--[if IE]>
<script src="../../../assets/ie.js"></script> <script src="../../../assets/ie.js"></script>
<![endif]--> <![endif]-->
<link rel="stylesheet" href="app.css">
<script src="app.js"></script> <script src="app.js"></script>
<script>require('initialize');</script> <script>require('initialize');</script>
</head> </head>
......
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