| Page 1 of 1 | [ 3 posts ] |
|
Hi. I use YUI3 on a daily basis, but recently I stumbled upon a problem in comparing nodes across multiple instances of YUI(). When I try to compare the same nodes (at least created from the same html node) created in different YUI instances it always looks like they are not the same.
Here is an example: Code: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Compare nodes across multiple YUI instances</title> <script type="text/javascript" src="http://yui.yahooapis.com/combo?3.1.1/build/yui/yui-min.js"></script> <script> var example = {}; YUI().use( 'node', function( Y ) { example.compareNode = function( refNode ) { var h1 = Y.one( 'h1' ); Y.log( h1 ); Y.log( refNode ); Y.log( h1.compareTo( refNode ) ); } } ); YUI().use( 'node', function( Y ) { var h1 = Y.one( 'h1' ); example.compareNode( h1 ); } ); </script> </head> <body> <h1>Compare nodes across multiple YUI instances</h1> </body> </html> In this example, the compareTo() function always returns false, even when the node elements are obviously the same. If the same code is within one YUI instance, compareTo() function returns true. Am I doing something wrong? Thanks in advance for any help or tip. |
|
Hey Ivan,
Every YUI instance is a sandbox, and objects created within a sandbox are unique. This includes Node objects as well. They are different objects even when then point to the same DOM element. If you really need to do such as comparation, you can do this: Code: alert(Y1.one ('#demo')==Y1.one(Y1.Node.getDOMNode( Y2.one('#demo') )); Keep in mind that the custom methods defined in the provider sandbox will not be ported over the consumer sandbox when you recreate a brand new object based on the DOM element. Best Regards, Caridy |
|
Thanks Caridy.
I know that the nodes are actually two different Node instances, but I hoped that the compare methods (compareTo, contains, ...) would check for the HTML elements live in DOM. caridy wrote: If you really need to do such as comparation, you can do this: Code: alert(Y1.one ('#demo')==Y1.one(Y1.Node.getDOMNode( Y2.one('#demo') )); Yes, that is exactly what I'm doing now |
| Page 1 of 1 | [ 3 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