PDA

View Full Version : Creating constraints from script


antun
07-18-2003, 09:43 AM
If you need to create a constraint based on a user action, you can do so using the applyConstraint() method:


<canvas>
<view x="100" width="20" height="20" bgcolor="blue">
<method event="oninit">
var f = function () {
this.setAttribute( "y" , m.y);
}
var d = [ m , "y" ];
this.applyConstraint( "y", f , d );
</method>
</view>

<view id="m" x="10" width="20" height="20" bgcolor="red">
<dragstate apply="true"/>
</view>
</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.

Enjoy!

naso
02-23-2008, 02:26 AM
Hi Antun,

Hope you'll get this message given how old this thread is.

My question is, when you add a constraint programmatically, as shown in your example, can you manipulate when the constraint is to be evaluated so that you make it 'immediately', 'once' or 'always'? In addition, what is the default value of the 'when' attribute?

Also in this post http://forum.openlaszlo.org/showpost.php?p=19364&postcount=4, spoco2 proposes an interesting way of defining a constraint. I am referring to the first code example. However, I can't get it working. Is this really a valid way of defining constraints?

Thanks in advance!

Naso.

antun
02-23-2008, 07:52 PM
When you add a constraint as shown above, it will be equivalent to "always". You can't specify "once" or "immediately", but neither would they make any sense in this case. "once" means "evaluate once at init time, and don't update", and that has already happened if the object exists. "immediately" means "evaluate at construct time", and that's even earlier than init time.

The default value of the when attribute is "always".

As for the example on the other thread, I'm not sure. I haven't tried creating constraints from script when instantiating objects (as shown), but it might work. It looks like there's a quote missing around the x-attribute.

Note that you can enable/disable constraints using states - that's a cleaner way to go if you don't need to parameterize the constraint's dependencies.

-Antun

naso
02-23-2008, 08:12 PM
Thanks Antun! It all makes sense now.

Cheers,

Naso