Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Commit

Permalink
trying to get redirect to All on Clear Completed on Completed filter
Browse files Browse the repository at this point in the history
working

doesn't actually work; got lost in a maze of EmberJS models,
controllers, routers, and things that seem to be a lot harder than they
should really be.
  • Loading branch information
soundasleep committed Jan 22, 2014
1 parent 5315a65 commit 45a31be
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
14 changes: 11 additions & 3 deletions index.html
Expand Up @@ -13,6 +13,7 @@
</head>
<body>
<script type="text/x-handlebars" data-template-name="todos">
{{this}}
<section id="todoapp">
<header id="header">
<h1>Todos</h1>
Expand Down Expand Up @@ -50,9 +51,8 @@ <h1>Todos</h1>
</ul>

{{#if hasCompleted}}
<button id="clear-completed" {{action "clearCompleted"}}>
Clear completed ({{completed}})
</button>
route = {{route}}
{{outlet button}}
{{/if}}
</footer>
</section>
Expand All @@ -63,6 +63,7 @@ <h1>Todos</h1>
</script>

<script type="text/x-handlebars" data-template-name="todos/index">
{{route}}
<ul id="todo-list">
{{#each itemController="todo"}}
<!-- binds 'completed' class to li if 'isCompleted' is 'true' -->
Expand All @@ -88,6 +89,13 @@ <h1>Todos</h1>
</ul>
</script>

<script type="text/x-handlebars" data-template-name="todos/button">
route = {{route}}
<button id="clear-completed" {{action "clearCompleted"}}>
Clear completed ({{completed}})
</button>
</script>

<!-- TODO use a Grunt script to combine all of these scripts into one -->
<script src="js/application.js"></script>
<script src="js/router.js"></script>
Expand Down
2 changes: 2 additions & 0 deletions js/controllers/todo_controller.coffee
Expand Up @@ -37,4 +37,6 @@ Todos.TodoController = Ember.ObjectController.extend(
)

isEditing: false

needs: ['todos']
)
4 changes: 4 additions & 0 deletions js/controllers/todos_controller.coffee
Expand Up @@ -27,6 +27,9 @@ Todos.TodosController = Ember.ArrayController.extend(
completed.invoke('deleteRecord')
completed.invoke('save')

# if we are currently on the /completed filter, we will have nothing remaining; redirect to the all filter
if @get('currentPath') is 'completed' then @transitionToRoute 'index'

# this is basically creating a runtime property/attribute that can be listened to?
remaining: ( ->
@filterBy('isCompleted', false).get('length') # implicit return
Expand All @@ -52,4 +55,5 @@ Todos.TodosController = Ember.ArrayController.extend(
remainingText: ( ->
(if @get('remaining') is 0 then "No" else @get('remaining'))
).property('remaining')

)
18 changes: 17 additions & 1 deletion js/router.coffee
Expand Up @@ -15,12 +15,20 @@ Todos.TodosRoute = Ember.Route.extend(
Todos.TodosIndexRoute = Ember.Route.extend(
model: ->
# return the same model as for the route 'todos' (i.e. return the same model as used in Todos.TodosRoute)
@modelFor 'todos' # implicit return; also the same as @modelFor('todos')
@modelFor 'todos'

# TODO test: can we create a renderTemplate as below, i.e. we get an implicit renderTemplate based on the naming convention of this route?
renderTemplate: (controller) ->
@render 'todos/index',
controller: controller

@render 'todos/button',
outlet: 'button',
controller: controller

setupController: (controller, model) ->
controller.set 'route', 'index'
controller.set 'model', model
)

Todos.TodosActiveRoute = Ember.Route.extend(
Expand All @@ -31,6 +39,10 @@ Todos.TodosActiveRoute = Ember.Route.extend(
renderTemplate: (controller) ->
@render 'todos/index',
controller: controller

@render 'todos/button',
outlet: 'button',
controller: controller
)

Todos.TodosCompletedRoute = Ember.Route.extend(
Expand All @@ -41,4 +53,8 @@ Todos.TodosCompletedRoute = Ember.Route.extend(
renderTemplate: (controller) ->
@render 'todos/index',
controller: controller

@render 'todos/button',
outlet: 'button',
controller: controller
)

0 comments on commit 45a31be

Please sign in to comment.