Convert or Suffer Endless Torment (aka why ‘Number()’ helps)

As you likely know, OpenLaszlo’s scripting language is based on Javascript - aka ECMAScript 3. Now, Javascript is a weakly dynamically typed language (<a href=”http://split-s.blogspot.com/2005/02/strongweak-vs-staticdynamic-typing.html”>debatable</a>) and has a habit of generating surprising results. Regardless of whether these ‘gotchas’ are a result of the weak dynamic typing or simply Javascript being Javascript, the consequence is that OpenLaszlo scripting is often a bit quirky.One quirk I just hit had to do with an animator producing funky results when used in conjunction with a data set. I was simply using the data set to initialize a view to a specified width. The problem was that when I specified the width directly (e.g. ‘150′), things worked as expected. However, when I substituted in the data path reference, all I got was junk. See for yourself:<h3>Hard Coded</h3><pre lang=”text”>width changed to150.150996008178width changed to150.691763283001width changed to151.617937604377width changed to154.079897270667width changed to161.488770028828width changed to179.951211667618width changed to207.510261932941width changed to235.523454865409width changed to275.229479425737width changed to290.190074409226width changed to295.099595336075width changed to298.583023698865width changed to299.67686271424width changed to300</pre><h3>Data Set</h3><pre lang=”text”>width changed to1500.274198962414936width changed to1500.2741989624149362.41547011242939width changed to1500.2741989624149362.415470112429394.77836342318196width changed to1500.2741989624149362.415470112429394.7783634231819614.2608297578881width changed to1500.2741989624149362.415470112429394.7783634231819614.260829757888135.7813996770265width changed to1500.2741989624149362.415470112429394.7783634231819614.260829757888135.781399677026545.4549816439118width changed to1500.2741989624149362.415470112429394.7783634231819614.260829757888135.781399677026545.454981643911826.9345067269413width changed to1500.2741989624149362.415470112429394.7783634231819614.260829757888135.781399677026545.454981643911826.934506726941313.4332687238966width changed to1500.2741989624149362.415470112429394.7783634231819614.260829757888135.781399677026545.454981643911826.934506726941313.43326872389665.31212008192width changed to1500.2741989624149362.415470112429394.7783634231819614.260829757888135.781399677026545.454981643911826.934506726941313.43326872389665.312120081921.19871422190803width changed to1500.2741989624149362.415470112429394.7783634231819614.260829757888135.781399677026545.454981643911826.934506726941313.43326872389665.312120081921.198714221908030.156146668481369width changed to150150</pre>It seemed odd that in the case of the data set usage, the animator continued to append new values onto the end of the string. Furthermore, these values seemed to be generally in-line with what the animator should be doing - animating the <code>width</code> up to twice its original value. Then it hit me - Javascript thought that the value in my <code>width</code> attribute was a string, and it did its best to ‘animate’ this string. The value ‘150′ was converted to an number automatically by the animator to perform the animation, and at each iterative step in the animation, the current delta was added on to the original value. However, although the delta was a number, the original value was a string. So, OpenLaszlo ended up concatenating the delta value on to the end of the existing original value, resulting in one nasty looking kindof correctish looking string. Now, that’s obviously not going to work, is it?<h3>The Solution</h3>Make sure you always convert (and validate) data from a data set before you use it. In my case, all I had to do was to add a call to <code>Number()</code> around my <code>datapath.xPathQuery()</code>:<h4>No Bueno</h4><pre lang=”xml”><handler name=”ondata”> awesomeView.setWidth(datapath.xpathQuery(”@awesome_width”)); coolView.setWidth(datapath.xpathQuery(”@cool_width”));</handler></pre><h4>Mucho Mejor</h4><pre lang=”xml”><handler name=”ondata”> awesomeView.setWidth(Number(datapath.xpathQuery(”@awesome_width”))); coolView.setWidth(Number(datapath.xpathQuery(”@cool_width”)));</handler></pre>

__LZgetNodes: p is null in Datapath

While slaving away on a recent data-intensive Laszlo project, I happened across the following error:<pre lang=”text”>INFO: __LZgetNodes: p is null in Datapath for container name: c id: c</pre>Great - another cryptic error message. I’ll spare you the link by link Googlechase and code archealogy that ensued and do what you wish your linear algebra tutor would do: just tell you the right answer.It turns out that the problem was in the following event handler:<pre lang=”xml”><handler name=”ondata”> util.setAttributeIfDefined(this, “height”, Number(datapath.xpathQuery(”project/@video_height”))); util.setAttributeIfDefined(this, “width”, Number(datapath.xpathQuery(”project/@video_width”)));</handler></pre>As I discovered, I was attempting to access a datapath that had yet to be linked to a data set. Note to Laszlo Systems: Could we get a bit of contextual help here? Like we’re spoiled with when we access a non-existent attribute? You know, like so:<pre lang=”text”>hello.lzx:2:20: attribute “size” not allowed at this point; ignoredhello.lzx:2:20: found an unknown attribute named “size” on element “text”, however there is an attribute named “fontsize” on class “text”, did you mean to use that?</pre><h3>The Solution</h3>Make sure you’ve actually got a data set associated with the datapath before you use the datapath. The easiest way to do this is to <strong>use an <code>ondata</code> event handler</strong> instead of <code>oninit</code>, <code>onload</code>, or any of their non-data aware brethren.

Just one more post

Some alternate text here!<>&&fejwifji2o@#

Not much here.

What a short story. Really.

That's it!

This is another post!’”

I\'m judging you!& %she %called it% “<Awesome!%>’ Yup, this is another fancy post, courtesy of Wordpress. What a beautiful system Wordpress is. And beauty is a good thing, isn’t it? Therefore, Wordpress is a good thing.

Testing 1, 2, 3

This is just a post. Nothing too exciting here!

Hello world!

Welcome to WordPress. This is your first post. Edit or delete it, then start blogging! replace foo bar.