无法删除Backbone.View导致僵尸

时间:2014-07-17 10:31:00

标签: backbone.js memory-management coffeescript views

我似乎无法完全破坏我的Backbone.Views。我已经尝试了所有的建议,但他们仍然变成了僵尸并开始对我的记忆启示。请告诉我我做错了什么。

当我在路径/访问和/设置之间来回切换时,浏览器会按预期创建新实例,close方法会触发并完成,但chrome中的探查器会显示先前实例所在的内容。

ZombieApocalypse

我的BaseView

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

我的BaseRouter

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

0 个答案:

没有答案