Re: example code not working - bugs found, still not working

Michael Franklin <[email protected]> Tue, 26 Apr 2005 01:11:17 +0000 (UTC)
Newsgroups gmane.comp.windows.devel.netwindows
Message-ID <[email protected]>
Michael Franklin <michael.franklin <at> peace.com> writes:

I have found some of the bugs in this code.  First of all, the assignment of the
testComp.onclick handler has to be a function pointer _not_ a string as shown in
the example:

    this.domNode.onclick = this.increment;
    //this.domNode.onclick =
"__components__.getComponentFromNode(this).increment();";

Secondly, the attempt at creating a 'private' method init() doesn't appear to be
syntactically correct.  

It should be this.init = function (){ ... }

testComp.increment is now called, but it still doesn't work.  I don't know how
to get hold of the NW_componentObject from the HTMLElement that 'this' points to
when increment() is called.  If I explicitly call 

var myObj = __components__.getComponent(5);

then the second component is found, and the counter is incremented and
redisplayed.  But why doesn't var myObj = __components__.getComponentFromNode(
this ); return the correct component? 

FYI I tried setting the NW_compID on the 'div' element
this.domNode.setAttribute( 'NW_compID',  this.globId );

Your help is appreciated,

Michael

<html>

<head>
<meta name=vs_targetSchema content="HTML 4.0">
<meta name=vs_defaultClientScript content="JavaScript">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>include test</title>

<!-- no netWindows objects are available up here -->
<script src="../../winScripts_no_docs/netWindows.js"language="JavaScript"
type="text/javascript"></script>
<!-- all netWindows core objects 
(including __sig__) are now in scope -->
<script language="JavaScript" type="text/javascript" >

function testComp( colour ){
  // we declare properties here just to be 
  // complete, this is not required in JavaScript
  this.domNode = null;
  this.textNode = null;
  this.counter = 0;

  // private initialization method 
  this.init = function (){
    this.domNode = document.createElement("div");
    this.domNode.onclick = this.increment;
    //this.domNode.onclick =
"__components__.getComponentFromNode(this).increment();";
    with(this.domNode.style){
      fontSize = "15px";
      fontFamily = "Arial, Helvetica, sans-serif";
      backgroundColor = colour;
      color = "white";
      width = "40px";
      height = "40px";
      textAlign = "center";
    }

    this.domNode.setAttribute( 'NW_compID',  this.globId );
    this.textNode = document.createTextNode(String(this.counter));
    this.domNode.appendChild(this.textNode);

    // add our visible representation to the page
	document.body.appendChild(this.domNode);
  }

  // public method called by event handler
  this.increment = function(){
    //var myObj = __components__.getComponent(5);
    var myObj = __components__.getComponentFromNode( this );
    if( myObj != null ) {
        if( myObj.textNode != null ) {  
            myObj.domNode.removeChild(myObj.textNode);
        }
        myObj.textNode = document.createTextNode(String(myObj.counter++));
        myObj.domNode.appendChild(myObj.textNode);
    }
  }

  // initialize the component
  this.init();
  // mixin-style inheritance:
  // used here to get every property of the parent class
  // but will not affect prototype chain
  NW_componentObj.call(this, this.domNode);
}

// prototype-style inheritance:
// unlike the mixin style, this ensures that 
// instanceof works correctly
testComp.prototype = new NW_componentObj();

__sig__.connectByName( __env__, "onLoad", null, "create");

function create() {
	// create some components
    var counter1 = new testComp( 'blue' );
    var counter2 = new testComp( 'green' );
	// you can now click on the counters and they 
   // increment independently
}

</script>



</head>

<body >

</body>

</html>






_______________________________________________
The netWindows developers list: [email protected]
http://netwindows.org/mailman/listinfo/devel_netwindows.org