Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make sure views are cleaned up after switching them

  • Loading branch information...
commit dc600662779df512402f0e7fbd5df4bd0a3644e5 1 parent 4848930
Emmanuel Delgado authored
2  assets/js/application.js
View
@@ -10,8 +10,6 @@
//= require_tree ./todo/routers
//
-_.extend(Backbone.View.prototype, App.ErrorHandlingHelpers);
-
$(document).ajaxError(function (event, xhr, options) {
if (xhr.status == 501) {
todoRouter.navigate('', { trigger: true, replace: true} );
2  assets/js/todo/collections/todos_collection.coffee
View
@@ -2,3 +2,5 @@
class App.TodosCollection extends Backbone.Collection
url: '/api/list_item'
+
+ model: App.TodoModel
6 assets/js/todo/models/session_model.coffee
View
@@ -1,7 +1,11 @@
@App ||= {}
class App.SessionModel extends Backbone.Model
- urlRoot: '/api/session'
+ initialize: ->
+ @id = 1
+
+ url: ->
+ '/api/session'
toJSON: ->
user: _.clone(@attributes)
6 assets/js/todo/models/todo_model.coffee
View
@@ -2,6 +2,12 @@
class App.TodoModel extends Backbone.Model
urlRoot: '/api/list_item'
+ defaults:
+ description: ''
+ priority: ''
+ completed: ''
+ due_date: ''
+
set: (attributes, options) ->
if attributes['completed'] == 'on'
attributes['completed'] = true
24 assets/js/todo/routers/router.coffee
View
@@ -3,16 +3,28 @@ class App.TodoRouter extends Backbone.Router
initialize: ->
@container = $('#container')
+ switchView: (view) ->
+ @currentView && @currentView.remove()
+ @currentView = view
+ @container.html(@currentView.render().el)
+
routes:
- '' : 'authenticate'
- 'todos' : 'todos'
+ '' : 'authenticate'
+ 'todos' : 'todos'
+ 'sign-out' : 'signOut'
authenticate: ->
- authenticationView = new App.AuthenticationView el: @container
- authenticationView.render()
+ @switchView(new App.AuthenticationView)
todos: ->
todosView = new App.TodosView
- el: @container
collection: new App.TodosCollection
- todosView.render()
+ @switchView(todosView)
+
+ signOut: ->
+ (new App.SessionModel id: 1).destroy
+ wait: true
+ error: (model, response, options) ->
+ todoRouter.navigate('', trigger: true, replace: true)
+ success: (model, response, options) ->
+ todoRouter.navigate('', trigger: true, replace: true)
2  assets/js/todo/templates/authentication.jst.ejs
View
@@ -1,7 +1,7 @@
<form id='authentication_view'>
<fieldset>
<legend>
- <%= title %> or <a href='' id='switch'><%= other_title %></a>
+ <%= title %> or <a href='#' id='switch'><%= other_title %></a>
</legend>
<div id='messages' name='error'></div>
4 assets/js/todo/templates/todos.jst.ejs
View
@@ -1,10 +1,10 @@
<div id='menu'>
<ul>
<li>
- <a href='' id='add-todo'>add</a>
+ <a href='#add-todo' id='add-todo'>add</a>
</li>
<li>
- <a href='' id='sign out'>sign out</a>
+ <a href='#sign-out' id='sign out'>sign out</a>
</li>
</ul>
</div>
16 assets/js/todo/views/error_handling_helpers.coffee → assets/js/todo/views/_base_view.coffee
View
@@ -1,6 +1,6 @@
@App ||= {}
-@App.ErrorHandlingHelpers =
+class App.BaseView extends Backbone.View
blockForm: ->
@$el.find('input').attr('disabled', 'disabled')
@@ -22,11 +22,11 @@
@unblockForm()
- getErrors: (response) ->
- if response.responseJSON && !_.isEmpty(response.responseJSON.errors)
- response.responseJSON.errors
- else
- { default: 'Unknown error' }
+ getErrors: (response) ->
+ if response.responseJSON && !_.isEmpty(response.responseJSON.errors)
+ response.responseJSON.errors
+ else
+ { default: 'Unknown error' }
- cleanErrors: ->
- @$el.find("[name=error]").html('')
+ cleanErrors: ->
+ @$el.find("[name=error]").html('')
9 assets/js/todo/views/authentication_view.coffee
View
@@ -1,5 +1,6 @@
@App ||= {}
-class App.AuthenticationView extends Backbone.View
+
+class App.AuthenticationView extends App.BaseView
template: JST['todo/templates/authentication']
events:
@@ -20,10 +21,10 @@ class App.AuthenticationView extends Backbone.View
initialize: ->
@signInFlow = true
- render: ->
+ render: (event) ->
+ event && event.preventDefault()
@$el.html(@template(@selectMessages()))
-
- return false
+ @
selectMessages: ->
@signInFlow = !@signInFlow
5 assets/js/todo/views/new_todo_view.coffee
View
@@ -1,5 +1,5 @@
@App ||= {}
-class App.NewTodoView extends Backbone.View
+class App.NewTodoView extends App.BaseView
template: JST['todo/templates/new_todo']
events:
@@ -22,7 +22,8 @@ class App.NewTodoView extends Backbone.View
@parent.render()
render: ->
- @$el.html(@template(@model.toJSON()))
+ @$el.html(@template(@model.toJSON().list_item))
+ @
changed: (event) ->
changed = event.currentTarget
5 assets/js/todo/views/todo_view.coffee
View
@@ -1,5 +1,5 @@
@App ||= {}
-class App.TodoView extends Backbone.View
+class App.TodoView extends App.BaseView
template: JST['todo/templates/todo']
tagName: 'ul'
@@ -13,7 +13,8 @@ class App.TodoView extends Backbone.View
@parent = options.parent
render: ->
- @$el.html(@template(@model.toJSON()))
+ @$el.html(@template(@model.toJSON().list_item))
+ @
destroy: (event) =>
event.preventDefault()
15 assets/js/todo/views/todos_view.coffee
View
@@ -1,5 +1,5 @@
@App ||= {}
-class App.TodosView extends Backbone.View
+class App.TodosView extends App.BaseView
template: JST['todo/templates/todos']
events:
@@ -18,24 +18,21 @@ class App.TodosView extends Backbone.View
todoView = new App.TodoView
model: model
parent: @
- @$el.find('#todos').append todoView.render()
+ @$el.find('#todos').append(todoView.render().el)
createTodo: (event) ->
event.preventDefault()
newTodoView = new App.NewTodoView
- el: @$el.find('#todos')
parent: @
- newTodoView.render()
+ @$el.find('#todos').html(newTodoView.render().el)
editTodo: (model) ->
newTodoView = new App.NewTodoView
- el: @$el.find('#todos')
parent: @
model: model
- newTodoView.render()
-
+ @$el.find('#todos').html(newTodoView.render().el)
render: ->
- @$el.html(@template())
@collection.fetch(reset: true)
-
+ @$el.html(@template())
+ @
Please sign in to comment.
Something went wrong with that request. Please try again.