Tuesday, February 28, 2006

The dreaded Java Applet

Q: Do people have problems with Java applets?

A: Yes

That's the short story. Upon seeing the dreaded "Java applet loading" animation playing in my browser window, I used to quickly hit the back button. But recently I've seen some nice demos at Processing.org that make me think there's some promise.

Where can you create a delegate? What is "this" in the script tag?

I needed a little timer function in Laszlo. I modified the Laszlo LzTimer demo to produce this:


<canvas height="100%" width="100%" debug="true">
  <attribute name="counter" type="number" value="10"/>
  <simplelayout axis="y" spacing="2" />
  <button text="start countdown" onclick="canvas.countdown(10)" />
  <text name="myText" width="${parent.width}"/>

  <script>
   Debug.write("doing countDelegate");
   this.countDelegate = new LzDelegate( this, "countdownText" );
  </script>

  <method name="countdown" args="val">
   Debug.write("countdown method with val = " + val);
   this.counter = val ;
   LzTimer.addTimer( this.countDelegate, 1000 );
  </method>

  <method name="countdownText">
   <![CDATA[
   Debug.write("doing countdownText " + this.counter);
   this.myText.setText( this.counter );
   if ( this.counter > 0 ) {
    LzTimer.resetTimer(this.countDelegate, 1000);
    this.counter--;
    Debug.write("decremented counter " + this.counter);
   }
   ]]>
  </method>
</canvas>

The code doesn't work as intended, and I get a warning on clicking the button: "WARNING: test_timer.lzx:15: reference to undefined property 'countDelegate'".

If I move the line this.countDelegate = new LzDelegate(this, "countdownText" ); from within the script tags into the countdown method, the application works correctly. If I had strictly followed the Laszlo demo, this is where I would have placed the LzDelegate constructor to begin with.

I can see from the debugging output that the script code is being run initially, so I don't understand why I'm getting this error. Maybe there's no "this" when the code within script is called? I'm off to investigate.


And I'm back. I added a little debugging code and found that in the script tag, "this" is printed as "[object Object]", while in method countdown, "this" is printed as "canvas". That's a clue, anyway.

Monday, February 27, 2006

Datasets are not allowed an id

I just tried to create a Laszlo dataset like this:

<dataset id="mydata"/>

and I got this compiler error:

"required attributes missing name is a required attribute of dataset; compilation aborted"

It appears that a dataset is not allowed the "id" attribute. But the compiler error is misleading; the "name" attribute is not required. You can in fact do this: <dataset />. That is discussed in the section on databinding of the Guide: "Datasets will automatically name themselves localdata if a name is not specified."

So there is at least one answer to my question "why not always use the id attribute instead of the name attribute?" There are cases where id is simply not allowed and you must use name as an identifier.

What is the difference between the name attribute and the id attribute?

This is not a question about HTML; it is a question about the Laszlo programming language. It has bothered me no end as I began running through Laszlo sample code, and I just found the answer today.

Q: What is the difference between the name attribute and the id attribute?

A: In Chapter 4 sec 2.2.2 of the Software Engineer's Guide to Developing OpenLaszlo Applications, we see

"In LZX the id attribute of an object is a global identifier that is visible throughout the entire program space, while the name of an object is an attribute like any other, which can only be referenced by its path (except in the case of named children of the canvas, as noted below)."

There's a simple example at their page, and here is some code that looks at a few different possibilities:
<canvas debug="true" width="800">
<simplelayout axis="y"/>
<view id="blue_id" name="blue_view" height="100" width="800" bgcolor="blue">
<simplelayout axis="y"/>
<!--
You can get the height of enclosing view by using its name or its id. Here I use its name.
-->
<text id="show_height_id" name="show_height" text="${blue_view.height}" bgcolor="yellow" width="800"/>
<!-- you cannot do this, show_height is not a child of canvas -->
<text text="${'Item show_height cannot be found by its name even here: ' + show_height.width}" width="800"/>
</view>
<!--
You can do the following, because the view "blue_view" is a "named child of the canvas". That is, it is a child of the canvas and it has a name attribute.
-->
<text text="${'Item blue_view is a named child of canvas; we can refer to it by name to get its height: ' + blue_view.height}" width="800"/>
<!-- you cannot do this; "show_height" is a child of view "blue_id" and is not visible here -->
<text text="${'Item show_height cannot be found by its name: ' + show_height.text}" width="800"/>
<!-- you can do this instead; the id of this item is globally visible -->
<text text="${'But item show_height_id can be found by its id: ' + show_height_id.text}" width="800"/>
</canvas>
If you run this code you see "undefined" anywhere it refers to an object that was out of scope.

Anyway, given all that, I wonder why give anything a "name"; if you need an identifier, just use "id". It's not like named objects have private scope; you can get to them by referring to their path (for example, in the above code, blue_id.show_height.text would work in place of show_height.text).

Learning a new programming language

I'm learning to program in Laszlo, and I think it's the hardest thing I've had to work with since I went from procedural to OO programming. Or maybe it just seems that way because I'm in the first gruesome stages of learning, where I'm seeing everything from an old frame which just doesn't apply to the new language.

I started last Sunday, dove right in and began developing by following some of the demo code that comes with the download. I'm not going about it in an orderly way - read all the documentation, follow the tutorials - because there's really no time for that. I develop, and then hunt through the documentation or demo code when I come to a roadblock. On the one hand, it offends my sense of aesthetics and order to develop this way. On the other hand, it's nice to see the results of my efforts turn into a real application in just a few short days.

I've got loads of questions, which I've been tracking. It's often difficult to find an answer immediately. I'll post these items along with any answers I've found.