[ 3 posts ]

Ivan Čentéš

  • Username: centi
  • Joined: Thu Aug 12, 2010 12:19 pm
  • Posts: 2
  • Offline
  • Profile
Tags:

Compare nodes across multiple YUI instances

Post Posted: Thu Aug 12, 2010 12:52 pm
+0-
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.

Caridy Patino

YUI Contributor

  • Username: caridy
  • Joined: Mon Dec 08, 2008 5:40 pm
  • Posts: 492
  • Location: Miami, FL
  • Twitter: caridy
  • GitHub: caridy
  • Gists: caridy
  • IRC: caridy
  • YUI Developer
  • Offline
  • Profile

Re: Compare nodes across multiple YUI instances

Post Posted: Thu Aug 12, 2010 3:57 pm
+0-
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

Ivan Čentéš

  • Username: centi
  • Joined: Thu Aug 12, 2010 12:19 pm
  • Posts: 2
  • Offline
  • Profile

Re: Compare nodes across multiple YUI instances

Post Posted: Fri Aug 13, 2010 12:12 am
+0-
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 :)
  [ 3 posts ]
Display posts from previous:  Sort by  
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