         el._jquery?.remove()
 
     tpl.on 'end', ->
-        tpl.jquery = tpl.xml._jquery = $(tpl.xml._children)
-        delete tpl.xml._children
+        tpl.jquery = tpl.xml._jquery
 
     old_query = tpl.xml.query
     tpl.xml.query = (type, tag, key) ->


# TODO i think this should work with asyncxml as well
# FIXME  this should be in an own package
# TODO listen on data and use innerHTML to create all dom elems at once
#       http://blog.stevenlevithan.com/archives/faster-than-innerhtml

jqueryify = (tpl) ->
    tpl.xml._children = []
    tpl.on 'add', (el) ->
        el._children = [] unless el._jquery?

    tpl.on 'close', (el) ->
        el._jquery = $(el.toString())
        # insert all children jquery parts
        if el._children
            # wtf? jquery's append doesnt eat a list???
            for child in el._children
                el._jquery.append child
            delete el._children
        # insert into parent
        if el.parent._children?
            el.parent._children.push el._jquery
        else
            el.parent._jquery.append el._jquery

        if el._jquery_delay?
            for delay in el._jquery_delay
                delay?()
            delete el._jquery_delay

        el.on 'newListener', (type) ->
            return if el._events?[type]?.length # dont bind two times for the same type

                return # dont return emit result (which is either true or false)

    tpl.on 'text', (el, text) ->
        settext = ->
            el._jquery.text(text)
        # only when tag is ready
        if el._jquery?
            settext()
        else
            el._jquery_delay ?= []
            el._jquery_delay.push(settext)


    tpl.on 'attr', (el, key, value) ->
        el._jquery?.attr key, value
