         $tree = getTreeElement(template)
         $tree.tree('removeNode', node)
 
-TemplateClass.destory = ->
-  _.each @checkboxes, ($checkbox) ->
-    # Remove bound events.
-    $checkbox.off()
-
-TemplateClass.events
-  'tree.select .tree': (e, template) -> handleSelectionEvent(e, template)
-  'tree.click .tree': (e, template) -> handleClickEvent(e, template)
-  'tree.dblclick .tree': (e, template) ->
-    id = e.node.id
-
-####################################################################################################
-# LOADING
-####################################################################################################
-
-loadTree = (element) ->
-  template = getTemplate(element)
-  $em = getElement(element)
-  getTreeElement(element).remove()
-  $tree = createTreeElement()
-  $em.append($tree)
-  model = template.model
-  docs = Collections.getItems(template.items)
-  treeData = model.docsToNodeData(docs)
-  settings = getSettings(element)
-  treeArgs =
-    data: treeData
-    autoOpen: settings.autoExpand
-    selectable: settings.selectable
-  treeArgs.onCreateLi = onCreateNode.bind(null, template)
-  $tree.tree(treeArgs)
-
-refreshTree = Functions.debounceLeft(loadTree, 1000)
+  template.isReactiveSetup = true
 
 ####################################################################################################
 # EXPANSION


getCheckedIds = (element) -> getTemplate(element).check.getIds()

uncheckAll = (element) ->
  return unless isCheckable(element)
  selectedIds = getTemplate(element).check.removeAll()
  handleCheckResult(element, {added: selectedIds, removed: []})

toggleChecked = (element, ids) ->
  return unless isCheckable(element)

  data = @data
  model = @model
  items = @items
  settings = getSettings()
  template = @
  # loadTree(@)
  refreshTree(@)

TemplateClass.destory = ->
  _.each @checkboxes, ($checkbox) ->
    # Remove bound events.
    $checkbox.off()

TemplateClass.events
  'tree.select .tree': (e, template) -> handleSelectionEvent(e, template)
  'tree.click .tree': (e, template) -> handleClickEvent(e, template)
  'tree.dblclick .tree': (e, template) ->
    id = e.node.id

####################################################################################################
# LOADING
####################################################################################################

loadTree = (element) ->
  template = getTemplate(element)
  $em = getElement(element)
  getTreeElement(element).remove()
  $tree = createTreeElement()
  $em.append($tree)
  model = template.model
  docs = Collections.getItems(template.items)
  treeData = model.docsToNodeData(docs)
  settings = getSettings(element)
  treeArgs =
    data: treeData.data
    autoOpen: settings.autoExpand
    selectable: settings.selectable
  treeArgs.onCreateLi = onCreateNode.bind(null, template)

  # Check all new items if needed.
  if settings.checked
    template.check.setIds(_.keys(treeData.visited.ids))

  $tree.tree(treeArgs)
  setUpReactiveUpdates(template)

# refreshTree = Functions.debounceLeft(loadTree, 1000)
refreshTree = _.debounce(loadTree, 1000)

setUpReactiveUpdates = (template) ->
  return if template.isReactiveSetup

  # Only a reactive cursor can observe for changes. A simple array can only render a tree once.
  cursor = Collections.getCursor(template.items)
  return unless cursor

  settings = getSettings(template)
  model = template.model

  template.autorun ->

    Collections.observe cursor,
      added: (newDoc) ->
        id = newDoc._id
        data = model.docToNodeData(newDoc, {children: false})
        # Check all new items if needed.
        if settings.checked then template.check.add([id])
        docParentId = model.getParent(data)
        unless docParentId
          # There is a significant memory leak caused by re-loading the entire tree after each
