PDA

View Full Version : Class with DataSet as a Param. Is there a better way?


TripleToe
06-15-2004, 01:39 PM
I'm trying to create a class which will basically be a panel that contains a listbox along with some buttons (to be added later). The list will be dynamically populated by a server-side datasource. I want to use this so that I can specify the dataset/datapath for the list of items when I declare the instance of the class.

Below, I've pasted what I have come up with based on the examples and input I've found in the forums. It does work but it feels very clumsey and I'm sure it is full of potential problems and inefficencies. Antrun, can you or anyone else look it over and tell me if there is a better way to refine this?

Thanks


<canvas bgcolor="0xeaeaea" width="800">

<!-- Data Set for loading list of subjects -->
<dataset name="subjectdataset" request="true" src="Subjects.xml" type="http"
querystring="action=all"/>

<!-- Data Set for loading list of teachers -->
<dataset name="teacherdataset" request="true" src="Teachers.xml" type="http"
querystring="action=all"/>

<!-- Generic list panel class -->
<class name="adminpanel" extends="view">
<!-- Title of the panel -->
<attribute name="paneltitle" value="null" type="string" />
<!-- DataPath for the list in the panel -->
<attribute name="listdatapath" value="null" type="string" />
<!-- Oninit method loads list with remote data from datapath passed as
an attribute in the instance tag -->
<method event="oninit">
this.mainview.itemlist.textlistitem.setDataPath(th is.listdatapath);
</method>
<!-- Text Banner at the top of the panel -->
<simplelayout axis="y" spacing="10" />
<statictext name="title" oninit="this.initText()" y="10" x="10">
<method name="initText">
this.setText(parent.getAttribute('paneltitle'));
</method>
</statictext>
<!-- List box -->
<view name="mainview">
<simplelayout axis="x" spacing="10" />
<list name="itemlist" shownitems="5" scrollable="true" width="300" height="75">
<textlistitem name="textlistitem" width="300" datapath="" text='$path{"name/text()"}'
value='$path{"@id"}' />
</list>
</view>
</class>
<!-- end of class definition -->


<!-- Instances of list panel displayed in tabs -->
<tabs id="system_administration_tabs" x="20" y="20">
<!-- Subjects Tab -->
<tabpane text="Subjects">
<adminpanel paneltitle="Subject List" listdatapath="subjectdataset:/subjects/subject"/>
</tabpane>
<!-- Teachers Tab -->
<tabpane text="Teachers">
<adminpanel paneltitle="Teacher List" listdatapath="teacherdataset:/teachers/teacher"/>
</tabpane>
</tabs>

</canvas>

antun
06-18-2004, 09:38 AM
It looks good to me. I didn't have the data so it didn't work for me. If you have the Teachers.xml and Subjects.xmls file and don't mind uploading them, I'd be happy to have a look at it again.

Can you elaborate a little on which parts you didn't feel comfortable with?

You componentized this well, and avoided having to meddle with the interior workings of your class to set the datapath to something more specific.


<method event="oninit">
this.mainview.itemlist.textlistitem.setDataPath( this.listdatapath );
</method>


Now this bit here could be simplified:

<statictext name="title" oninit="this.initText()" y="10" x="10">
<method name="initText">
this.setText(parent.getAttribute('paneltitle'));
</method>
</statictext>


... to:


<statictext name="title" text="${parent.paneltitle}" y="10" x="10" />


Other than that it looks great.

-Antun

TripleToe
06-18-2004, 01:08 PM
Thanks for the reply. I guess I was just looking for some input on whether I was moving in the right direction. The things that bother me the most with this (and the code I've written since) is:

1. Because I'm using classes, I have to avoid using the ID attribute (which would cause conflicts when I use multiple instances of the class) so I end up with lots of "this.parent.parent.parent.parent....." references. Is there a way to work around this or perhaps make a reference within the class definition from the top down, starting with the instance as the root of the reference?

2. The application that I'm working on will have lots of interactivity with the back-end servers. Because of this, I wonder if there might be some advantage to using the persistent connection. Does it change the asynchronous request paradigm at all and would this make coding easier in any way? I'm having to learn to write code asynchronously now, following the model of "use one dataset to make the request, and set up a datapointer to await the response". I wish I could make backend calls synchronously (ie like mm remoting), but I'm coming to realize that this is just how Laszlo is setup. (By the way, I beta tested the competition and I find Laszlo to be FAR superior.)

In case you want it, I've pasted a sample of the XML from Subjects.xml below. Keep in mind that this file does not really exist. I'm using Cocoon to generate XML dynamically from my database which seems to work pretty well!


<?xml version="1.0" encoding="ISO-8859-1"?>
<subjects>
<subject id="11">
<name>Algebra</name>
</subject>
<subject id="12">
<name>Algebra II</name>
</subject>
<subject id="9">
<name>Band</name>
</subject>
<subject id="14">
<name>Band II</name>
</subject>
<subject id="8">
<name>British Literature</name>
</subject>
<subject id="10">
<name>Chemistry</name>
</subject>
</subjects>

Thanks for the help!

antun
06-18-2004, 01:47 PM
... so I end up with lots of "this.parent.parent.parent.parent....." references.

Inside of a class definition, you can use "classroot" to refer to the top of the class. That might make your life easier. Note that you can't use it in the root of the class - there it means the root of the surrounding class, if there is one.

2. The application that I'm working on will have lots of interactivity with the back-end servers. Because of this, I wonder if there might be some advantage to using the persistent connection.[quote]

Unless you're actually writing a chat application, I would stick to making data requests the "traditional" way.

[quote]I wish I could make backend calls synchronously

I do understand exactly what you're saying, but I wonder how (or if) this problem could be solved:

Hypothetically speaking, say that dataset had a doSynchronousRequest() method that told the LFC to wait for the response before proceeding with the next line of the script. The request would get made, but how would you handle errors? Say the request timed out?

(By the way, I beta tested the competition and I find Laszlo to be FAR superior.)

Glad to hear that!

-Antun

TripleToe
06-18-2004, 02:04 PM
Hypothetically speaking, say that dataset had a doSynchronousRequest() method that told the LFC to wait for the response before proceeding with the next line of the script. The request would get made, but how would you handle errors? Say the request timed out?

Coming from the Java world, my first instinct would be to use a try/catch syntax. That would allow for error handling and if the server threw exceptions on a timeout, that would work for those as well. However, this implies that ECMA script would support such features. I also have to keep in mind that the asynchronous model used by Laszlo is not inherently bad, but just different from what I'm used to working with. Once I get the hang of it, I'm sure it won't be as big of an issue.

What would really help me out would be to find some best practices and design patterns that apply to Laszlo. I've searched the forums and the sample code and I've found a few examples, but in general, there seems to be a limited number of examples that demonstrate a reusable framework for creating LZX classes that rely heavily on remote request calls using datasets/datapointers to server-side sources. I've found examples of remote calls in some of the sample apps, but it would be nice to see some design patterns for creating these type of applications. Maybe I'm just not looking in the right place.

Thanks again
:)