Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Brunch with Chaplin. #639

Merged
merged 4 commits into from
Jul 31, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions labs/dependency-examples/chaplin-brunch/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
bower_components/
!bower_components/todomvc-common/

# NPM packages folder.
node_modules/

Expand Down
52 changes: 3 additions & 49 deletions labs/dependency-examples/chaplin-brunch/app/application.coffee
Original file line number Diff line number Diff line change
@@ -1,67 +1,21 @@
Chaplin = require 'chaplin'
mediator = require 'mediator'
routes = require 'routes'
HeaderController = require 'controllers/header-controller'
FooterController = require 'controllers/footer-controller'
TodosController = require 'controllers/todos-controller'
Todos = require 'models/todos'
Layout = require 'views/layout'

# The application object
module.exports = class Application extends Chaplin.Application
# Set your application name here so the document title is set to
# “Controller title – Site title” (see Layout#adjustTitle)
title: 'Chaplin • TodoMVC'

initialize: ->
super

# Initialize core components
@initDispatcher controllerSuffix: '-controller'
@initLayout()
@initMediator()

# Application-specific scaffold
@initControllers()

# Register all routes and start routing
@initRouter routes, pushState: no
# You might pass Router/History options as the second parameter.
# Chaplin enables pushState per default and Backbone uses / as
# the root per default. You might change that in the options
# if necessary:
# @initRouter routes, pushState: false, root: '/subdir/'

# Freeze the application instance to prevent further changes
Object.freeze? this

# 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}

# Instantiate common controllers
# ------------------------------
initControllers: ->
# These controllers are active during the whole application runtime.
# You don’t need to instantiate all controllers here, only special
# controllers which do not to respond to routes. They may govern models
# and views which are needed the whole time, for example header, footer
# or navigation views.
# e.g. new NavigationController()
new HeaderController()
new FooterController()
new TodosController()

# Create additional mediator properties
# -------------------------------------
initMediator: ->
# Create a user property
mediator.user = null
# Add additional application-specific properties and methods
mediator.todos = new Todos()
# If todos are fetched from server, we will need to wait
# for them.
mediator.todos.fetch()
# Seal the mediator
mediator.seal()
super
17 changes: 4 additions & 13 deletions labs/dependency-examples/chaplin-brunch/app/assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,13 @@
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Chaplin & Brunch • TodoMVC</title>
<link rel="stylesheet" href="../../../../assets/base.css">
<title>Chaplin &amp; Brunch • TodoMVC</title>
<link rel="stylesheet" href="../bower_components/todomvc-common/base.css">
<link rel="stylesheet" href="../bower_components/todomvc-common/base.js">
<!--[if IE]>
<script src="../../../assets/ie.js"></script>
<![endif]-->
<link rel="stylesheet" href="stylesheets/app.css">

<!-- Usually all these files are concatenated automatically
by brunch and you need just to import `vendor.js` -->
<script src="../../../../assets/base.js"></script>
<script src="../../../../assets/jquery.min.js"></script>
<script src="../../../../assets/lodash.min.js"></script>
<script src="../../../../assets/handlebars.min.js"></script>

<script src="javascripts/vendor.js"></script>
<script src="javascripts/app.js"></script>
<script src="app.js"></script>
<script>require('initialize');</script>
</head>
<body>
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
Controller = require 'controllers/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
title: 'Todo list'
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

list: (options) ->
@publishEvent 'todos:filter', options.filterer?.trim() ? 'all'
# 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) ->
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

This file was deleted.

5 changes: 3 additions & 2 deletions labs/dependency-examples/chaplin-brunch/app/initialize.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Application = require 'application'
routes = require 'routes'

# Initialize the application on DOM ready event.
$ ->
app = new Application()
app.initialize()
new Application
controllerSuffix: '-controller', pushState: false, routes: routes
13 changes: 0 additions & 13 deletions labs/dependency-examples/chaplin-brunch/app/lib/support.coffee

This file was deleted.

12 changes: 0 additions & 12 deletions labs/dependency-examples/chaplin-brunch/app/lib/utils.coffee

This file was deleted.

39 changes: 0 additions & 39 deletions labs/dependency-examples/chaplin-brunch/app/lib/view-helper.coffee

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('chaplin').mediator
module.exports = Chaplin.mediator

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Model = require 'models/base/model'

module.exports = class Todo extends Model
# It is a very good idea to have base Model / Collection
# e.g. Model = require 'models/base/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:
title: ''
completed: no
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
Collection = require 'models/base/collection'
Todo = require 'models/todo'

module.exports = class Todos extends Collection
module.exports = class Todos extends Chaplin.Collection
model: Todo
localStorage: new Store 'todos-chaplin'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Chaplin = require 'chaplin'
View = require 'views/base/view'

module.exports = class CollectionView extends Chaplin.CollectionView
# This class doesn’t inherit from the application-specific View class,
# so we need to borrow the method from the View prototype:
getTemplateFunction: View::getTemplateFunction
useCssAnimation: true
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
Chaplin = require 'chaplin'
require 'lib/view-helper' # Just load the view helpers, no return value

module.exports = class View extends Chaplin.View
# Precompiled templates function initializer.
getTemplateFunction: ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
View = require 'views/base/view'
template = require 'views/templates/footer'
View = require './base/view'
template = require './templates/footer'

module.exports = class FooterView extends View
autoRender: yes
autoRender: true
el: '#footer'
events:
'click #clear-completed': 'clearCompleted'
listen:
'todos:filter mediator': 'updateFilterer'
'all collection': 'renderCounter'
template: template

initialize: ->
super
@subscribeEvent 'todos:filter', @updateFilterer
@modelBind 'all', @renderCounter
@delegate 'click', '#clear-completed', @clearCompleted

render: =>
render: ->
super
@renderCounter()

updateFilterer: (filterer) =>
updateFilterer: (filterer) ->
filterer = '' if filterer is 'all'
@$('#filters a')
.removeClass('selected')
.filter("[href='#/#{filterer}']")
.addClass('selected')

renderCounter: =>
renderCounter: ->
total = @collection.length
active = @collection.getActive().length
completed = @collection.getCompleted().length
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
View = require 'views/base/view'
template = require 'views/templates/header'
View = require './base/view'
template = require './templates/header'

module.exports = class HeaderView extends View
autoRender: yes
autoRender: true
el: '#header'
events:
'keypress #new-todo': 'createOnEnter'
template: template

initialize: ->
super
@delegate 'keypress', '#new-todo', @createOnEnter

createOnEnter: (event) =>
ENTER_KEY = 13
title = $(event.currentTarget).val().trim()
return if event.keyCode isnt ENTER_KEY or not title
@collection.create {title}
@$('#new-todo').val ''
createOnEnter: (event) =>
ENTER_KEY = 13
title = $(event.currentTarget).val().trim()
return if event.keyCode isnt ENTER_KEY or not title
@collection.create {title}
@$('#new-todo').val ''
10 changes: 0 additions & 10 deletions labs/dependency-examples/chaplin-brunch/app/views/layout.coffee

This file was deleted.

Loading