           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

        # to be removed.
        return unless node
        removeSelection(template, [id])
        $tree = getTreeElement(template)
        $tree.tree('removeNode', node)

TemplateClass.destory = ->
