Skip to content
Browse files

Delete a Movie with Backbone

When a Movie is deleted, we want to clean up both the list and detail
views.

A view first hides and then removes itself from the DOM. We also want
the view to clean up after itself - this app only uses the basic
Backbone.View events, more complex apps will probably need to use the
unbind method to undo complex bindings (to Backbone objects, jQuery,
etc).
  • Loading branch information...
1 parent 5066f50 commit 61035021b958e2d46b8bb7664b361887d8d7db29 @leshill committed Jun 27, 2012
View
7 app/assets/javascripts/app/cache.js.coffee
@@ -6,6 +6,8 @@ class App.Cache
@_byModelCid[other.model.cid] = other
@_byCid[other.cid] = other
+ other.model.on 'destroy', @_modelDestroyed, @
+
get: (model) ->
@_byModelCid[model.cid]
@@ -18,3 +20,8 @@ class App.Cache
reset: ->
@_byModelCid = {}
@_byCid = {}
+
+ _modelDestroyed: (model, collection, options) ->
+ other = @get(model)
+ @remove(model)
+ other.destroy?()
View
4 app/assets/javascripts/app/collection_detail_view.js.coffee
@@ -7,9 +7,13 @@ class App.CollectionDetailView extends App.View
@view = options.view
+ @collection.on 'destroy', @destroy, @
@selectionModel.on 'select:none', @hideDetail, @
@selectionModel.on 'select', @showDetail, @
+ destroy: (model, collection, options) ->
+ @currentView = null if @currentView.model == model
+
hideDetail: ->
@currentView?.hide()
View
12 app/assets/javascripts/app/view.js.coffee
@@ -4,6 +4,13 @@ class App.View extends Backbone.View
@presenter ||= options.presenter
@selectionModel = options.selectionModel
+ destroy: ->
+ @hide()
+ @undelegateEvents()
+ @unbind()
+ @$el.remove()
+ @
+
events: ->
{}
@@ -40,5 +47,10 @@ class App.View extends Backbone.View
@$el.show()
@
+ unbind: ->
+ # TODO
+ # events bindings are handled by undelegateEvents()
+ # undo other bindings (for example to models or jQuery)
+
_template: (template) ->
HoganTemplates[template]
View
7 app/assets/javascripts/app/views/show.js.coffee
@@ -6,6 +6,13 @@ class App.Views.Show extends App.View
event.preventDefault()
App.router.navigate '/', trigger: true
+ deleteMovie: (event) ->
+ event.preventDefault()
+ if window.confirm('Are you sure?')
+ @model.destroy()
+ App.router.navigate '/', trigger: true
+
events: ->
_.extend super,
'click a.back': 'backToList'
+ 'click a.delete': 'deleteMovie'
View
2 app/assets/javascripts/templates/show.hamstache
@@ -10,5 +10,5 @@
%a(href = '/' class = 'back')
Back to list
.destroy
- %a(href = '' data-confirm = 'Are you sure?' data-method = 'delete' rel = 'nofollow')
+ %a(href = '' class = 'delete')
Delete Movie?
View
6 app/controllers/movies_controller.rb
@@ -1,6 +1,6 @@
class MoviesController < ApplicationController
- respond_to :html, only: [:destroy, :index, :show]
- respond_to :json, only: [:create]
+ respond_to :html, only: [:index, :show]
+ respond_to :json, only: [:create, :destroy]
def create
new_movie.save
@@ -9,7 +9,7 @@ def create
def destroy
movie.destroy
- redirect_to root_path
+ respond_with movie
end
def populate

0 comments on commit 6103502

Please sign in to comment.
Something went wrong with that request. Please try again.