[Laszlo-dev] Detecting classes

Rami Ojares / AMG rami.ojares at archon.fi
Sun Jan 17 20:21:52 PST 2010


Hi Tucker, 

When 4.7 was released the restructuring of tagname etc. forced me to detect Pane tags as per your suggestion.
Currently I can´t get it to work.

Here's the situation:

I have a createChildren method overridden in SplitPane class
And from there I am calling the method you said would detect Pane classes
(I only changed c.class to c['class'] because compiler told me to. And I put the declaration
of variable outside of if statement)

Now the isPane seems to return false everytime.
Since I don't really understand what is going on in here I ask for your guidance and assistance.
 
<method name="createChildren" args="children"><![CDATA[
    
    var splitpaneChildren = [];
    var paneCount = 0;
    
    for(var i=0; i<children.length; i++) {
        
        var pane = children[i];
        var divider;
        
        if (isPane(pane)) {
            
            divider = {
                'class': lz.Divider,
                attrs: {
                    width: this.dividerWidth,
                    index: paneCount
                }
            };
            
            // add index attribute also to Pane
            pane.attrs.index = paneCount;
            
            splitpaneChildren.push(divider);
            splitpaneChildren.push(pane);
            paneCount++;
        }
        else splitpaneChildren.push(pane);
        
    }
    super.createChildren(splitpaneChildren);
    
]]></method>

<method name="isPane" args="c"><![CDATA[
    Debug.debug("c = %w", c);
    
    var childClass;
    if (c.name) childClass = lz[c.name];
    else childClass = c['class'];
    
    Debug.debug("childClass = %w", childClass);
    
    var ret = lz['Pane'].prototype.isPrototypeOf(childClass.prototype);
    Debug.debug("returning %w", ret);
    return ret;
]]></method>

- rami
> If you are trying to detect instances of <Pane> (even anonymous ones, which would be subclasses), the recommended approach would be to use the `is` operator as above.  But I see where your problem lies, you are trying to sort the child "specifications" before they have been instantiated.  To be totally accurate, that means you would have to look for `name` _or_ `class`, and that you would need to know if the tag or class is a subclass of the tag's class you are sorting on.
>
> Something like:
>
> function isPane(c) {
>   if (c.name) {
>     var childClass = lz[c.name];
>   } else {
>     var childClass = c.class;
>   }
>   return lz['Pane'].prototype.isPrototypeOf(childClass.prototype);
> }
>
> [That last line is pretty obscure, we should probably offer a built-in `extends` or `isSubclassOf` predicate.]



More information about the Laszlo-dev mailing list