PDA

View Full Version : invoking an app with a default data query


lyndonwong
10-26-2003, 03:49 PM
Hi Antun,

I'm trying to modify pWeather_widget to start up with a weather report for a default Zip Code defined in the external parameters XML file. Can you help me understand why the following code does not work as intended? I tried numerous placements of oninit="this.getWeather()" to no avail.

thx,

Lyndon

p.s. -- forgive lack of tag indentation, i couldn't figure out how to preserve indents (see other post). Attempted code edit in RED text.

--------------------------------

<canvas width="184" height="204" debug='false'>

<!-- RESOURCES -->

<font name="serif" src="timmonsr.ttf" />
<font name="san" src="helmetr.ttf" />

<!-- Check for pWeather_url value or use default -->
<script>
if (typeof(pWeather_url) == 'undefined') pWeather_url='pWeather_parameters.xml';
</script>

<!-- pWeather XML FILE set to parameter defined above -->
<dataset name="pWeather_parameters" type="http" autorequest="true" src="${pWeather_url}" />

<!-- XML datasource for U.S. weather, provided by Laszlo Systems -->
<dataset type="http" name="weatherdata" src="http://www.laszlosystems.com/cgi-pub/weather.cgi" />


<!-- APP INTERFACE -->

<!-- Widget border created via two nested views -->

<view x="3" y="3" width="180" height="200" bgcolor="gray" >

<view width="178" height="198" x="1" y="1" bgcolor="white" datapath="pWeather_parameters:/parameters/" >

<simplelayout axis="y" />
<text x="6" y="2" height="22" font="san" fontsize="14" width="170" fgcolor='${parseInt(parent.colorValue.text)}' datapath='titleText/text()' />

<!-- Query user for input of a U.S. zip code, and submit data request via HTTP Get (default) -->
<view x="8" y="1" font="san" fontsize="12" oninit="this.getWeather()">
<simplelayout axis="x" />
<windowtext width="90" valign="bottom" name="inputField" datapath='zipCode/text()'></windowtext>
<method name="getWeather" >
var data = canvas.datasets.weatherdata;
data.setQueryString({ zip : parent.inputField.getText() });
data.doRequest();
</method>
<button width='70'> Enter ZIP
<method event="onclick">
var data = canvas.datasets.weatherdata;
data.setQueryString({ zip : parent.inputField.getText() });
data.doRequest();
</method>
</button>
</view>
<view height="12"></view>

<!-- Display formatting for returned weather report -->
<view x="6" width="170" clip="true" font="serif" fontsize="12" name="WeatherReport" datapath="weatherdata:/weather[1]/current" >
<simplelayout axis="y" />
<text fgcolor="black" fontsize="14" width="166" datapath="where/text()" />
<text fgcolor="black" fontsize="14" width="166" datapath="desc/text()" />
<text fgcolor="green" fontsize="24" width="166" datapath="temp/text()" />
<text fgcolor="gray" width="166" datapath="station/text()" />
<text fgcolor="gray" width="166" datapath="when/text()" />
</view>

<!-- EXTRACTED PARAMETERS FROM: pWeather_parameters.xml -->
<text name='colorValue' fontsize="12" width="170" fgcolor='white' datapath='titleColor/text()' />

</view>
</view>
</canvas>

-------------------------------------------------
pWeather_parameters.xml file below:


<parameters>
<titleText>Weather Report</titleText>
<titleColor>0x008000</titleColor>
<zipCode>94107</zipCode>

</parameters>

antun
10-27-2003, 08:42 AM
I think the most useful way for me to answer this is to show you how to debug it:

1) If you leave the debugger on, you'll see an error in it:

default.lzx:37: reference to undefined property 'inputField'
default.lzx:37: undefined object does not have a property 'getText'

If you go to line 37 in the code you pasted, you'll see that it's actually the line in your getWeather() method that sets the querystring of the dataset to your default value:

data.setQueryString({ zip : parent.inputField.getText() });

The "undefined object" error suggests to me that either:

a) The object is misreferenced (not very likely, since it's a very simple address: parent.inputField; if it were a more complex address, then I might suspect that), or...
b) The inputField view does not exist at the time that you reference it.

So I added some debug statements to your code:


<windowtext width="90" valign="bottom" name="inputField"
datapath='zipCode/text()'
oninit="debug.write('inputField inits now')"
ondata="debug.write('inputField data now')" />
<method name="getWeather" >
debug.write( 'getWeather called' );
var myds = canvas.datasets.weatherdata;
debug.write( 'The zip is: ' + parent.inputField.getText() );
myds.setQueryString({ zip : parent.inputField.getText() });
myds.doRequest();
</method>


... and if you look at the debugger, you'll see that:

a) The zip isn't set.
b) The ondata event (i.e. the moment that the zip is displayed in inputField) of inputField happens AFTER the getWeather() method is called.

I'm not completely sure why you've got the default zip in a separate XML file - certainly it's possible to do this, but it might be overkill. It's far simpler to hard-code the default zip code in:


<method name="getWeather" >
var defaultZip = 94107;
var myds = canvas.datasets.weatherdata;
myds.setQueryString({ zip : defaultZip });
myds.doRequest();
</method>


-Antun