| Page 1 of 1 | [ 7 posts ] |
Josh LizarragaYUI Contributor
|
Hi all,
According to the YUI3 Widget page, "Parents automatically render their children when rendered". I'm sure I am missing something simple here, but I'm having some difficulty getting this to work. In the simple example below, what is required for div#test to be recognized as a TestChild instance and have its boundingBox rendered? Code: <!doctype html> <html> <head> <meta charset="utf-8"> <title>YUI3 Widget Family - Parent-Child Test</title> </head> <body> <div id="demo-child"> <p>TestChild Content #1</p> </div> <div id="demo-parent"> <p>TestParent Content</p> <div id="test"> <p>TestChild Content #2</p> </div> </div> <script src="http://yui.yahooapis.com/3.3.0/build/yui/yui-min.js"></script> <script> YUI().use('widget', 'widget-parent', 'widget-child', function(Y){ Y.TestChild = Y.Base.create('test-child', Y.Widget, [Y.WidgetChild]); Y.TestParent = Y.Base.create('test-parent', Y.Widget, [Y.WidgetParent], {}, { ATTRS: { defaultChildType: { value: Y.TestChild } } }); new Y.TestChild({ srcNode: '#demo-child' }).render(); new Y.TestParent({ srcNode: '#demo-parent' }).render(); }); </script> </body> </html> Thanks! |
Juan Ignacio DopazoYUI Contributor
|
My understanding is that the Widget page is specifying an standard. You should implement the auto-render of children in your parent class yourself.
Take a look at TabView's render lifecycle https://github.com/yui/yui3/blob/master ... ew.js#L117 |
Josh LizarragaYUI Contributor
|
Huh. Well, I don't feel as dumb now, lol. The WidgetParent feature list on that YUI3 Widget page is bound to confuse some novices like me given the seemingly built in/out of the box nature of most of the features listed.
Could someone help offer some insight into which of those features a) really come "out of the box" or b) have to implemented in your own code? I will answer what I can; hopefully this topic can become a resource for Googlers like me until there are some guides available.
PS: Thank you very much Juan for answering my Widget questions this past week. This has been a bit of a steep learning curve for me and you've been very helpful. |
Juan Ignacio DopazoYUI Contributor
|
No problem!
The selection support is a way of marking child widgets as "selected", which could mean various things. In a TabView, the selected tab is the visible one. WidgetParent has a configuration attribute called "multiple" that specifies whether more than one child can be selected at the same time. If more than one child is selected, getting the "selection" attribute from the parent will return an ArrayList with all the selected children. This comes all out of the box. I also haven't tried the event bubbling scheme. |
Josh LizarragaYUI Contributor
|
By complete happenstance, I think I may have uncovered some more clues about this - and it 's looking like the rendering of child widgets *is* supposed to happen automatically, out of the box.
Based on your advice and the render lifecycle of TabView, I added the renderUI() method to my example above, then added a generic method called _renderChildren(), following the _renderTabs() method in TabView's renderUI(). I added console.log() statements to each of these methods to make sure they were being called properly before I added code to them, and to my surprise I found my _renderChildren() method being called twice (or once if I removed the renderUI() method entirely). I could only think of one reason for this, and sure enough WidgetParent comes with a built-in _renderChildren() method: https://github.com/yui/yui3/blob/master ... nt.js#L777 So as long as your WidgetParent is aware of its children *before* the render lifecycle, it looks like those children would indeed be automatically rendered. That makes me think the "correct" solution to this problem is to query for and add the children during the initialization lifecycle. I will give this a try and report back with my results. Perhaps TabView handles this differently because its child relationships are more complex? I.e., it has two types of children, the tabs and the panels, each with their own properties and behaviors. |
Josh LizarragaYUI Contributor
|
Success! Adding the children at the initialization lifecycle phase of the WidgetParent leads to automatic, out of the box WidgetChild rendering.
See below for a complete example: Code: <!doctype html> <html> <head> <meta charset="utf-8"> <title>YUI3 Widget Family - Parent-Child Test</title> </head> <body> <div id="demo-child"> <p>TestChild Content #1</p> </div> <div id="demo-parent"> <p>TestParent Content</p> <div id="test"> <p>TestChild Content #2</p> </div> </div> <script src="http://yui.yahooapis.com/3.3.0/build/yui/yui-min.js"></script> <script> YUI().use('widget', 'widget-parent', 'widget-child', function(Y){ Y.TestChild = Y.Base.create('test-child', Y.Widget, [Y.WidgetChild]); Y.TestParent = Y.Base.create('test-parent', Y.Widget, [Y.WidgetParent], { initializer: function(){ this._addChildren(); }, _addChildren: function(){ var parent = this, children = parent.get('contentBox').all('> div'); children.each(function(child){ parent.add({ contentBox: child }); }); } }, { ATTRS: { defaultChildType: { value: Y.TestChild } } }); new Y.TestChild({ srcNode: '#demo-child' }).render(); new Y.TestParent({ srcNode: '#demo-parent' }).render(); }); </script> </body> </html> Thanks again Juan, couldn't have figured it out without your help! |
|
Hey Josh,
What is the point of having a child widget outside of the scope of the parent in the DOM structure? Do you have a use-case? Now, if you just want to auto-render a child using a custom selector instead of relying on the default behavior (rendering children collection under the parent's context box), I remember Satyen introduced a way to handle that a while ago, I can give you more details if needed. Just keep in mind that the general use case will work like this: Code: new Y.TestParent({ srcNode: '#demo-parent', children: [ { foo: 'first child' }, { foo: 'second child' } ] }).render(); The main reason for this is to allow the parent to control the lifecycle of the children collection. Best Regards, Caridy |
| Page 1 of 1 | [ 7 posts ] |
| You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum |
© 2006-2013 Yahoo! Inc. All rights reserved.
All code on this site is licensed under the BSD License unless stated otherwise.
About This Site · Security Contact Info
Powered by phpBB® Forum Software © phpBB Group