Data Binding in AS3

Copy link to clipboard
Copied
Hi All,
I've very new to flash and i'm also using flex 4.5.
I currently have on mxml 2 static componenets : HSlider and TextInput.
The textinput's text property is binded to the hslider's value property.
Here is their code:
<s:HSlider id="periodsSlider"
thumbRelease="periodsSlider_thumbReleaseHandler(event)"
minimum="1"
maximum="30"
showDataTip="false"
dataTipPrecision="1"
x="80" y="36"
width="132" height="11"
/>
<s:TextInput id="periodsText"
change="textinput1_changeHandler(event)"
text="{periodsSlider.value}"
x="80" y="10"
width="132"
/>
I need to create them dinamicaly with AS3 but i'm having toubles setting the binding property right.
Here is the AS3 code:
// create the periods slider
var tmpSlider:HSlider = new HSlider;
tmpSlider.minimum = 1;
tmpSlider.maximum = 30;
tmpSlider.id = "periodsSlider";
tmpSlider.addEventListener(TrackBaseEvent.THUMB_RELEASE , periodsSlider_thumbReleaseHandler);
tmpSlider.showDataTip = false;
tmpSlider.dataTipPrecision = 1;
tmpSlider.width = 130
tmpSlider.height = 10
tmpSlider.x = 80;
tmpSlider.y = tmpInput.y + 25;
mainPanel.addElement(tmpSlider);
// create the periods input text
var tmpInput:TextInput = new TextInput;
tmpInput.x = 80;
tmpInput.y = currentY;
tmpInput.width = 130;
tmpInput.addEventListener(TextOperationEvent.CHANGE , textinput1_changeHandler);
tmpInput.id = "periodsText";
mainPanel.addElement(tmpInput);
How should I modify it to have the data binding correctly?
Thanks In advance.
Ravid
Message was edited by: ravid.paldi
Copy link to clipboard
Copied
One way is to listen to the change event from the HSlider and on the event update the TextInput text with HSlider value.
--
Kenneth Kawamoto

Copy link to clipboard
Copied
If I understand correct, your advice is to a "periodsSlider_thumbReleaseHandler" function.
But how do I referance the input text inside the function?
if i try this code:
protected function periodsSlider_thumbReleaseHandler(event:TrackBaseEvent):void
{
periodsText.text = event.target.value.toString();
}
I get a compiler error: "Access of undefined property periodsText" so how do i referance a compenent which was created in run time?
On that subject, what's the difference of using the "bindSetter" function from the "binding.utils"?
Thanks,
Ravid
Copy link to clipboard
Copied
According to your previous post, it's "tmpInput" not "periodsText".
Data binding using AS
http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf69084-7cc9.html
You can use this method in your case too if you prefer.
--
Kenneth Kawamoto

Copy link to clipboard
Copied
Hi Kenneth,
I thought to use "periosText" because it was the ID of the component, I tried to replace it with the compnent's name - "tmpInput" as you suggested and still same error. It make sence since tmpInput is a local var therefor deleted after the function is finished (currect me if i'm worng)
The code is:
protected function periodsSlider_thumbReleaseHandler(event:TrackBaseEvent):void
{
tmpInput.text = event.target.value.toString();
}
Any other ideas?
About the link, i've found this page. and many others but they all show adding databinding to a component that was created with MXML and i'm lookinf for databinding between 2 components that were created with action script. which currntly i can't even change their proprty not to talk abotu data binding.
I don't understand why it is so complicated. it sounds like a very common task 😞
Thanks,
Ravid
Copy link to clipboard
Copied
I don't think you can treat component id in AS like a nomal variable like that.
What do you mean local variable? Is it defined in a function? Then move it outside.
There shouldn't be much differences in creating components in MXML or in AS - I haven't got a time to do a demo right now though sorry
--
Kenneth Kawamoto

