Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
121 views
in Technique[技术] by (71.8m points)

famo.us - How to remove nodes from the Render Tree?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 transitions

var renderController = new RenderController();
renderController.show( .... );
renderController.hide( .... );

See 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 a render function. The Flipper class is a simple example (and the RenderController is a complex example of this pattern)

In depth explanation:

  1. Every RenderNode has a render function which creates a renderSpec.
  2. The renderSpec contains information about a Modifier or Surface.
    • The Modifier specs are used to calculate the final CSS properties.
    • The Surface specs are coupled to DOM elements.
  3. Every tick of the Engine, the renderSpec is rendered using the RenderNode.commit function.
  4. The commit function uses the ElementAllocator (from the Context) 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 a render function will suffice. The View 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

 var mod = new ShowModifier({visible:true});
 mod.visible = true;
 mod.show();
 mod.hide();

or, as alternative, use this gist to add visibility functions to Modifier and StateModifier

 modifier.visibleFrom(function(){ return true; }) // function, getter object or value
 stateModifier.setVisible(true); // or false

WARNING: Adding/removing DOM-nodes by manipulating the renderspec might cause a performance penalty!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...