           return
         sortResults = getSortedIndex(template, newDoc)
         nextSiblingNode = sortResults.nextSiblingNode
+        $tree = getTreeElement(template)
         if nextSiblingNode
           $tree.tree('addNodeBefore', data, nextSiblingNode)
         else

        childDocs = model.getChildren(newDoc)
        data.children = _.map childDocs, (childDoc) ->
          getNode(template, childDoc._id)
        $tree.tree('updateNode', node, data)
        parent = newDoc.parent
        if parent != oldDoc.parent

  treeArgs.onCreateLi = onCreateNode.bind(null, template)
  $tree.tree(treeArgs)

# TODO(aramk) Put into utility.
debounceLeft = (func, delay) ->
  isPressed = true
  handle = null
  press = ->
    isPressed = true
    setTimeout((-> isPressed = false), delay)
  wrapped = ->
    if isPressed
      press()
      return
    result = func.apply(null, arguments)
    press()
    result
  wrapped

refreshTree = debounceLeft(loadTree, 1000)

####################################################################################################
# EXPANSION
