am 20.9.2001 1:39 Uhr schrieb Adrian Kubala unter [EMAIL PROTECTED]:

> I've noticed that objects in a tree are precreated twice. First
> flagPrecreate traverses the tree doing precreation in post-order, then
> create does the same thing indirectly. Normally this doesn't cause
> problems but I'm working on a tree widget and it's causing wierd
> problems.

ok, we are talking about the following code:

DynObject.prototype.create = function() {
    this.flagPrecreate();   // (1)
    this.specificCreate();
    this.created = true;
    var l = this.children.length;
    for(var i=0;i<l;i++) this.children[i].create()  // (2)
    this.invokeEvent("create");
};
DynObject.prototype.flagPrecreate = function() {
    var l=this.children.length;
    for (var i=0; i<l;  i++) this.children[i].flagPrecreate();  // (3)
    this.invokeEvent('precreate');  // (4)
};

flagPrecreate is meant to invoke the 'precreate'-Events (4). now for the
reason that the 'precreate'-Event is invoked multiple times on the same
layer: both methods traverse the subtree recursively (2),(3). flagPrecreate
is called from create (1), and this call is the problem, because
flagPrecreate has already traversed the whole subtree after (1), but create
gets called again (2) and therefore also flagPrecreate.
i hope i made this clear.

to avoid the problem described above i would suggest to move the
flagPrecreate call outside out the create-methode into the addChild-method.
addChild then would look like this:

DynObject.prototype.addChild = function(c) {
    if(c.isChild) c.parent.removeChild(c);
    c.isChild = true;
    c.parent = this;
    if(this.created) {
        this.flagPrecreate();
        c.create();
    }
    this.children[this.children.length] = c;
    return c;
};

btw:
i never use the 'precreate'-Events, because the same effect can be achieved
much more simpler.
consider the following widgets:
1. using 'precreate'-Event:

GreenLyr = function() {
    this.superClass = DynLayer; this.superClass();
    this.addEventListener(GreenLyr.listener);
};
GreenLyr.prototype = new DynLayer;
GreenLyr.listener = new EventListener();
GreenLyr.listener.onprecreate = function(e) {
    var o = e.getSource();
    o.setBgColor('green');
    o.setSize(100,100);
};

2. without using the 'precreate'-Event:

GreenLyr = function() {
    this.superClass = DynLayer; this.superClass();
    this.setBgColor('green');
    this.setSize(100,100);
}; 
GreenLyr.prototype = new DynLayer;

these two versions do exactly the same thing. which one does look simpler?

> Also: I've noticed some of the core code using event handlers
> gratuitously in place of methods. With any event, there are some things
> which are essential to the nature of the event; without them, the event
> is meaningless. There are other things which are auxilliary to the
> event, which are triggered by it. The former belong in a method, the
> latter in listeners. As it is, there are several methods which don't do
> anything but dispatch events; all the real work is done in a listener,
> defeating the key principle of OO; that operations on data should go
> together with the data upon which they operate.
> 
> It's an academic argument until you want to, say, extend the class and
> override the code in the listener. Or you're just trying to understand
> the code and you have to bounce all around different files because the
> meat of that method is actually in a listener somewhere else.

ACK! i noticed that too. i usually use the listeners only to call a method
on the object. 

--
Michael Buerge


_______________________________________________
Dynapi-Dev mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dynapi-dev

Reply via email to