问题
I would like to be able to know when all views have been rendered in a layout. The reason I want to do this is because I need to be able to reference positions of rendered views in the layout, so that I may animate views relative to other views that had already been rendered.
I have read this: Surface render events in famo.us
One way I can think of is to count views that have been rendered and after all had been rendered (based on a known count), I can then assume all have been rendered. Obviously this would not work if there's an unknown number of views to render.
So another way to achieve this would be to have the layout fire a custom event when all of its managed surfaces have completed rendering. It seems like a good place to do this would be in a function shim in the given layout. For example, in SequentialLayout it would be SequentialLayout.prototype.commit
. But, I am not 100% certain on whether that's the right approach, because it looks like the Engine
or Entity
manages the rendering. So, what that smells like to me is that this approach wouldn't actually fire the event I want fired at the right time.
Are there any best practices for this?
回答1:
The best way in your use case may be to listen for the deploy on each surface (renderable) in each view
surface.on('deploy', function() {...});
and once all of the surfaces have been deployed emit a custom event to the view
thisView._eventOutput.emit('completed-deploy', {data: someData});
You can then listen to each view to know when the renderables are deployed
view.on('completed-deploy', function(data) {....});
Although this does take care of the fact that the renderable is deployed to the DOM, it will not let you know if it is done with any transition of a modifier. In the case you would also need to have a function for flagging when transition is complete in your views, if that is what you need.
来源:https://stackoverflow.com/questions/27681117/what-is-a-good-way-to-know-when-all-surfaces-had-been-initially-rendered-in-famo