我似乎无法完全破坏我的Backbone.Views。我已经尝试了所有的建议,但他们仍然变成了僵尸并开始对我的记忆启示。请告诉我我做错了什么。
当我在路径/访问和/设置之间来回切换时,浏览器会按预期创建新实例,close方法会触发并完成,但chrome中的探查器会显示先前实例所在的内容。
class BaseView extends Backbone.View
# -------------------------------------------
isRendered: false
autoDestroy: false
# -------------------------------------------
close: ->
log 'Closing View', @, @model
@onClose() if @onClose?
@remove() if @?
@unbind() if @? # Unbind all local event bindings
delete @$el # Delete the jQuery wrapped object variable
delete @el # Delete the variable reference to this node
delete @
log 'Everything is destroyed', @
# -------------------------------------------
module.exports = BaseView
class Router extends BaseRouter
name: "Router"
container: "#content-container"
routes: routes
#----------------------
access: (slug) ->
AccessPage = require '../view/page/access-page.coffee'
accessPage = new AccessPage()
accessPage.render()
@showPage accessPage
#----------------------
settings: (slug) ->
SettingsPage = require '../view/page/settings-page.coffee'
settingsPage = new SettingsPage()
settingsPage.render()
@showPage settingsPage
class BaseRouter extends Backbone.Router
currentPageView: null
pageClass: '.page'
# -------------------------------------------
showPage: (view, hidePages = true) ->
@hideAllPages() if hidePages
@currentPageView = view
$(@container).append view.$el
log 'showPage', view.$el
view.$el.show()
# -------------------------------------------
hideAllPages: ->
if @currentPageView?.onHide?
@currentPageView.onHide()
if @currentPageView? and @currentPageView.autoDestroy
@currentPageView.close()
$(@pageClass).hide()
class AccessPage extends BaseView
name: "AccessPage"
id: "access-page"
autoDestroy: true
className: 'page'
#----------------------
initialize: ->
log @name, "initialize"
#----------------------
render: ->
log @name, "render"
@$el.html template
title: "Access"
@isRendered = true
#----------------------
module.exports = AccessPage