Yes - every Ractive instance has two methods that allow you to do this: ractive.detach()
and ractive.insert()
. Unfortunately the documentation is currently lacking, but here's how you use it:
// remove the instance from the DOM, and store a document
// fragment with the contents
docFrag = ractive.detach();
// insert the instance into the container element, immediately
// before nodeToInsertBefore (the second argument is optional -
// if absent or `null` it means 'insert at end of container'
ractive.insert( container, nodeToInsertBefore );
If you're removing the instance and immediately reinserting it, there's no need to detach it first - you can just do ractive.insert()
. The arguments can be DOM nodes, but they can also be CSS selectors or the IDs of elements.
Here'a a JSFiddle demonstrating: http://jsfiddle.net/rich_harris/Uv8WJ/
You can also do this with inline components (i.e. <subcomponent/>
as opposed to new Subcomponent()
. In this JSFiddle, we're using ractive.findComponent('subcomponent')
method to get a reference to the instance: http://jsfiddle.net/rich_harris/f28t5/.