I feel like I'm missing something obvious, but how do I remove nodes from the render tree and destroy them correctly?
It looks like I can just do something like mainCtx._node._child.splice(2,1)
, but this doesn't work in all cases (Scrollviews seem to stick around), and assume there's something relevant in the API but I can't seem to find it.
You never remove renderNodes - you use smart RenderNodes to manipulate what is rendered.
The solution depends on what you want to accomplish:
1) I want to manipulate the layout
The easiest way to show / hide / swap parts of the RenderTree is to use a
RenderController
. You can even specify in/out transitionsSee the official example
2) I want to manage performance (and remove stuff I don't need)
Don't worry about removing nodes. Famo.us will manage this for you.
If you want to take control of rendered nodes, write a custom
View
with arender
function. The Flipper class is a simple example (and the RenderController is a complex example of this pattern)In depth explanation:
RenderNode
has arender
function which creates a renderSpec.Modifier
orSurface
.Modifier
specs are used to calculate the final CSS properties.Surface
specs are coupled to DOM elements.Engine
, the renderSpec is rendered using theRenderNode.commit
function.commit
function uses theElementAllocator
(from theContext
) to allocate/deallocate DOM elements. (Which actually recycles DOM nodes to conserve memory)Therefore: Just return the correct renderSpec in your custom
View
, and famo.us will manage memory and performance for you.BTW, you don't need to use the
View
class - an object with arender
function will suffice. TheView
class simply adds events and options which is a nice way to create encapsulated, reusable components.Update: Ready-made Solutions
ShowModifier (gist) a simple modifier to show/hide parts of the rendering tree
or, as alternative, use this gist to add visibility functions to
Modifier
andStateModifier
WARNING: Adding/removing DOM-nodes by manipulating the renderspec might cause a performance penalty!