[Laszlo-dev] Setting constraints on a newed object

achim bode achimbode at gmx.net
Fri Jul 22 10:57:50 PDT 2005


hi P T,

   are there any means to clean constraints once you have created them
   this way?

   I tried this:

    <class name="mutual_constraint_manager">
        <attribute name="cleanup_array" type="expression" value="$once{new Array()}"/>
        
        <method name="init">
            // add methods to parent like a state:
            parent.set_constraint = this.set_constraint;
            parent.set_mutual_constraint = this.set_mutual_constraint;
        </method>
        
        <method name="set_mutual_constraint" args="my_attr, other_obj, other_attr">
            // register in both directions
            this.set_constraint(my_attr, other_obj, other_attr);
            other_obj.set_constraint = this.set_constraint;
            other_obj["set_constraint"](other_attr, this, my_attr);
            // disable the last delegate (otherwise it will only
               // update in one direction)
            var last_update_event = other_obj._events[this._events.length-1];
            last_update_event.delegateList[1].disable();
            // collect references to instantiated objects for later
               // cleanup
            this.cleanup_array.push(last_update_event);
            this.cleanup_array.push(last_update_event.delegateList[0]);
            this.cleanup_array.push(last_update_event.delegateList[1]);
        </method>
        <method name="set_constraint" args="my_attr, other_obj, other_attr">
            debug.write(" before:");
            debug.write(this._events);
            var func = function () {   
                this.setAttribute(my_attr, other_obj[other_attr]);       
            }
            this.cleanup_array.push(func);
            var dependency = [this, my_attr, other_obj, other_attr];
            this.applyConstraint(my_attr, func, dependency);
            debug.write(" after:");
            debug.write(this._events);
        </method>
        <method name="clean_constraints">
            <![CDATA[
            var to_be_destroyed;
            while(cleanup_array.length > 0){
                to_be_destroyed = cleanup_array.pop();
                debug.write("to_be_destroyed: " + to_be_destroyed);
                to_be_destroyed.destroy();
            }
            ]]>
        </method>
    </class>

    but this is only a blind guess about what has been created and
    registered. and it is obviously not all you needed to remove. in
    my own tests at least became slower any time I used it...

Regards,
          Achim

ps: for all others - just in case anyone is interested:
this is the simple version which works for wiring in both directions
(I don't know why, but it does ;o).

<canvas debug="true">
    <!-- test_mutual_constraint.lzx -->
    <debug x="150" height="400"/>
    <class name="mutual_edittext" extends="edittext">
        <method name="set_mutual_constraint" args="my_attr, other_obj, other_attr">
            this.set_constraint(my_attr, other_obj, other_attr);
            other_obj.set_constraint = this.set_constraint;
            other_obj["set_constraint"](other_attr, this, my_attr);
            var last_update_event = other_obj._events[this._events.length-1];
            last_update_event.delegateList[1].disable();
        </method>
        <method name="set_constraint" args="my_attr, other_obj, other_attr">
            var func = function () {   
                this.setAttribute(my_attr, other_obj[other_attr]);       
            }
            var dependency = [this, my_attr, other_obj, other_attr];
            this.applyConstraint(my_attr, func, dependency);
        </method>
    </class>

    <edittext name="first" x="5" y="5"/>
    <mutual_edittext name="second" x="5" y="35">
        <method event="oninit">
            this.set_mutual_constraint('text', parent.first, 'text');
        </method>
    </mutual_edittext>
    
    <!-- only there to be able to set the focus somewhere else... -->
    <edittext name="third" x="5" y="70"/>
</canvas>


--------------------
Thursday, July 21, 2005, 3:39:12 PM, you wrote:

PTW> Good to see someone is reading the documentation!  I didn't realize  
PTW> that applyConstraint was public, but it it certainly the right way to  
PTW> go (better than the way Adam and I proposed) because it will also  
PTW> take care of managing the delegates that get created so they don't  
PTW> leak, _and_ it will make sure that the constraint is executed at  
PTW> least once (which Adam and I missed, too).

PTW> Thanks Achim!

PTW> On 21 Jul 2005, at 09:20, achim bode wrote:

>> hi Andrew,
>>
>> there is a general description on:
>> http://www.laszlosystems.com/lps-2.2/docs/guide/ 
>> constraints.html#d0e10373
>>
>> applyConstraint()
>>
>> The following example uses the applyConstraint() method inherited  
>> from LzNode in order to create a constraint at runtime. You'll see  
>> that the setConstraint method has the same effect as the simple  
>> expression y=${m.y}
>>
>> Code Sample:
>>
>> <canvas>
>>   <view x="250" width="20" height="20" bgcolor="red"
>>         y="${m.y}"/>
>>
>>   <view x="300" width="20" height="20" bgcolor="blue">
>>     <method name="setConstraint" event="oninit">
>>       var f = function () {
>>         this.setAttribute("y", m.y);
>>       }
>>       var d = [m, "y"];
>>       this.applyConstraint("y", f, d);
>>     </method>
>>   </view>
>>
>>   <window id="m" x="10" title="Drag me" width="160" height="20"/>
>> </canvas>
>>
>> f is a callback function that is required for the applyConstraint()  
>> method.
>> d is an array consisting of a pointer to a reference node, and the  
>> attribute to bind to.
>>
>>
>> Regards,
>>           Achim
>>
>> --------------------
>> Tuesday, July 19, 2005, 6:52:46 PM, you wrote:
>>
>> AM> What's the best way to set up constraints on a javascript-created
>> AM> object?
>>
>> AM> e.g.   convert <LZClass height="40" width="${parent.width}" />  
>> into
>> AM> javascript
>>
>> AM>   var obj = new LZClass(this, {height: 40});
>>
>> AM> ... then what?
>>
>>
>> AM> andrew
>>
>> AM> _______________________________________________
>> AM> Laszlo-dev mailing list
>> AM> Laszlo-dev at openlaszlo.org
>> AM> http://www.openlaszlo.org/mailman/listinfo/laszlo-dev
>>
>>
>> _______________________________________________
>> Laszlo-dev mailing list
>> Laszlo-dev at openlaszlo.org
>> http://www.openlaszlo.org/mailman/listinfo/laszlo-dev
>>
>>

PTW> _______________________________________________
PTW> Laszlo-dev mailing list
PTW> Laszlo-dev at openlaszlo.org
PTW> http://www.openlaszlo.org/mailman/listinfo/laszlo-dev




More information about the Laszlo-dev mailing list