Make Flex TextInput show no prompt on empty String - actionscript-3

I am using a s:TextInput in Flex 4.5. It shows it's prompt text if the underlying text value is null or empty String. Does anybody know if I can make either don't show the prompt on empty String or even show a different prompt?
I already found a way by extending the TextInput class and overriding some of the methods but I am still hoping anyone here knows an easier way ;-)

Ok, so based on the comments, here it is:
You store the current prompt value in a private variable, like so:
private var _inputPrompt:String = "";
Then you create a getter, so the value is accessible from outside of this class:
public function get inputPrompt():String
{
return _inputPrompt;
}
Now you can bind inputPrompt anywhere you need it, however, the problem is the getter won't be recalled once the private value changes. You can fix this very easily: Create an update method, for example like so:
public function updateInputPrompt(value:String):void
{
_inputPrompt = value;
}
Ok, nothing fancy so far. I'm guessing this is the point where you are right now. In order to "force" the getter to be recalled, you have to bind it to an event, like so:
[Bindable(event="inputPromptUpdated")]
public function get inputPrompt():String
{
return _inputPrompt;
}
Finally, you can simply dispatch this event when the value is update (i.e. in the updateInputPrompt method):
public function updateInputPrompt(value:String):void
{
_inputPrompt = value;
dispatchEvent("inputPromptUpdated"); // For binding
}
This way, the getter will be recalled every time you dispatch that event.
Hope this helps. Have a great day, and a great weekend!

Related

How to have protected set functions?

I'm working in ActionScript 3, but I'm guessing this is a universal concept.
I've recently stumbled upon the world of get and set functions, and I immediately tried to apply it to my code, since before I'd need to run an explicit set function so that two parameters would keep up to date with each other.
However, I only ever actually SET the variable in a subclass to where the variable is defined - so I'd like it to be protected. Unfortunately, the code seems to only work when the set function is public, and I have no clue. All the examples I could find online never used a set function except outside of the class it was defined in.
This isn't so much something that's going to be difficult to work around (I would just use an explicit set function) as something I want to understand to become a better programmer, and maybe help someone else who is in my position.
Here's my code which might make my point a bit clearer:
private var _iColumn:int;
private var _iRow:int;
public function get iColumn():int { return _iColumn; }
public function get iRow():int { return _iRow; }
protected function set iColumn(setValue:int):void {
_iColumn = setValue;
resetClickArea();
}
protected function set iRow(setValue:int):void {
_iRow = setValue;
resetClickArea();
}
public function TileElement(column:int, row:int)
{
iColumn = column; // Complains that property is read-only
iRow = row; // Complains that property is read-only
}
Both getter and setter need to have the same namespace (public, protected or private) so if the getter needs to be public your only option is to make the setter public as well or replace the setter by a protected function like setIRow or something.
Making both getter and setter protected would make valid code but of course you won't be able to access the the property from outside.
Most languages don't even have this nice feature of get and set keywords. You would be better off creating your own getter and setter functions getRow(); and setRow(r);. One other bonus is that you don't get too attached to this AS3 feature, and it would be easier to work within other languages like C++.

AS3 Profiling - Why do Object and Function counts increase when setting text

Firstly can some one please explain what is meant by Object and Function in a profiling environment.
Secondly, why does the Object and Function count increase when I repeatedly set the text property of a textfield:
override public function setLanguage(id:String):void
{
if (id == "en")
{
ui.title.text = _data.text.title.en;
ui.title.direction = Direction.LTR;
}
else if (id == "ae")
{
ui.title.text = _data.text.title.en;
ui.title.direction = Direction.RTL;
}
}
From Laurent:
Internally, TextField::text is most likely a getter/setter (since it needs to set a flag to update the text field display, also possibly update the HTML content, etc.) so when you set it you are effectively calling a function.
This means that TextField.text is implemented as a property getter and setter, so if you had to code it, you would see something like
private var _text:String="";
public function get text():String {
return _text;
}
public function set text(value:String):void {
_text=value;
}
Your Object count increases every time you reference (looking for a better word, don't kill me about this :P) an object (I trust you know what objects are), and your Function count increases every time you invoke a function.
So when you do something like
myTextField.text="Hello World";
you are referencing object myTextField and invoking its function set text(String);, causing your counts to increase by 1 each.
Internally, TextField::text is most likely a getter/setter (since it needs to set a flag to update the text field display, also possibly update the HTML content, etc.) so when you set it you are effectively calling a function.
What is it you don't understand about the difference between a Function and an Object? Could you be more specific?

AS3: How to know if dataprovider or its content(s) is changed

I'm implementing some kind of combobox control (by extending spark.components.supportClasses.DropDownListBase)
Now, inside this control; I need to know:
if the dataprovider is changed/assigned. (which I can do... the first approach below works);
if any item in the dataprovider collection has changed.
I tried 2 methods that did not do the trick...
1ST APPROACH:
[Bindable("collectionChange")]
override public function set dataProvider(value:IList):void
{
if (value) value.addEventListener(CollectionEvent.COLLECTION_CHANGE, onDataChange);
super.dataProvider = value;
trace("DATA CHANGED"); //fires
}
protected function onDataChange(event:CollectionEvent):void
{
trace("COLLECTION ITEM(S) CHANGED"); //does not fire
}
2ND APPROACH:
Since this is based on DropDownListBase; it should dispatch the CollectionEvent.COLLECTION_CHANGE event already..?
public function myClass() //constructor
{
addEventListener(CollectionEvent.COLLECTION_CHANGE, onDataChange);
}
protected function onDataChange(event:CollectionEvent):void
{
trace("DATA CHANGED"); //does not fire
}
Any ideas?
UPDATE: Edited above.. The first approach lets me know if the dataprovider is changed but not if any item is updated in the dataprovider collection. The second approach does not work at all..
We'll be able to help significantly more if you show us a runnable sample to demonstrate the problem.
if the dataprovider is
changed/assigned.
Your first approach should work. Can you tell us what makes you think it didn't? ( No trace statement I assume? ). And tell us what you did to change the dataProvider.
The second approach won't work because myClass is not firing off the collectionChange event.
2 if any item in the dataprovider collection has changed.
There is not really a way to tell this. In most cases, a collection is just a list of pointers to other objects. If you change those pointers, then a collectionChange event is fired. IF you change the item that he pointer is pointed to, the collection has no way to know that something changed. Binding works very similarly if your an MXML fan.
If you have control over how items are changed, you can deal with it that way. Instead of:
(collection.getITemAt(x) as myObject).property = newValue;
Do something like this:
var myObject : MyObject = collection.getITemAt(x) as myObject
myObject.property = newValue;
collection.setItemAt(x, myObject);
I would expect that to fire a collectionChange event, but not the former.
That said, in the context of a dropDownListBase: As you scroll or open and close the drop down, the itemRenderers should be updated to reflect the most current dataProvider's data. But if you change something on the fly while the drop down is open, I would not expect it to update automatically [if you're not changing the dataProvider.

checking if child exists

Hello i have a function as following:
private function seatClickHandler(e:MouseEvent):void{
var check:Check = new Check();
if(e.target.contains(check)){
e.target.removeChild(seat);
}else{
e.target.addChild(check);
}
}
basicly i want to check if e.target contains a child called check. If it does i want e.target to remove the child, else i want to add the child. But the method i tried doesnt seem to work although i think this is the way to go. Any suggestions?
When you declare your Check object, Actionscript creates a reference code for that specific object.
So the first time your code is run, your Check object could be given a reference of #c0ecc29. Your if statement checks to see if #c0ecc29 is a child component of target. It won't be, so the Check object with reference #c0ecc29 is added to target.
The second time the clickHandler is called, a new instance of the Check object is created which will have a new reference id. Your target has the original Check object with the #c0ecc29 reference so it won't get removed.
The correct way to get this working depends on what target is (DataGrid, Group, etc.).
EDIT:
Based on your comments, I would try something like this. It checks to see if the Check object is a child of target and adds it if needed. Then when the Check object is clicked, it will toggle its visibility.
public var check:Check = new Check();
private function seatClickHandler(e:MouseEvent):void
{
if(!e.target.contains(check))
{
check.addEventListener(MouseEvent.CLICK, check_handleClick);
e.target.addChild(check);
}
}
protected function check_handleClick(event:MouseEvent):void
{
check.visible = !check.visible;
}
If you need to actually remove the Check object from target instead of just changing its visibility, you could try this:
public var check:Check = new Check();
private function seatClickHandler(e:MouseEvent):void
{
if(!e.target.contains(check))
{
e.target.addChild(check);
}
else
{
e.target.removeChild(check);
}
}
If the child is named 'check' then you should be able to use getChildByName(). See flash.display.DisplayObject.name
If you happen to have the child in memory, you can use getChildIndex()
check is a new object in the scope of that function, so it will not be a child of the event target.
What you want to do is declare check as a global variable (And also cast target as DisplayObjectContainer).
e.g.
private function seatClickHandler(e:MouseEvent):void{
if((e.target as DisplayObjectContainer).contains(check)){
(e.target as DisplayObjectContainer).removeChild(seat);
}else{
(e.target as DisplayObjectContainer).addChild(check);
}
}
However I'm not sure if this is exactly what you want to do (There can only be one check). A better approach would be to have a function (maybe toggleCheck) on the target, and have that display object responsible for rendering the check (And removing it)
This worked perfectly fine for me in my situation:
if(possibleChild.parent == holder)
holder.removeChild(possibleChild)
It may or may not be exactly what you're looking for.

Flash, ActionScript 3: using get/set properties to get values from other classes creates much duplicate code can it different?`

i am using get and setters in my as3 code to edit values of an other class (because those variables are shared) i dont like to put stage.sharedVar.isScrabble in my code every time to change a variable so i used get/set functions
see below
private function get isScrabble(){return stage.sharedVar.isScrabble;}
private function set isScrabble(val){stage.sharedVar.isScrabble = val;}
private function get superGrid(){return stage.sharedVar.superGrid}
private function set superGrid(val){stage.sharedVar.superGrid = val;}
private function get gridSize(){return stage.sharedVar.gridSize}
private function set gridSize(val){stage.sharedVar.gridSize = val}
private function get blokDefaultWidth(){return stage.sharedVar.blokDefaultWidth}
private function set blokDefaultWidth(val){stage.sharedVar.blokDefaultWidth = val}
private function get blokDefaultHeight(){return stage.sharedVar.blokDefaultHeight}
private function set blokDefaultHeight(val){stage.sharedVar.blokDefaultHeight = val}
as you van see it has a lot of duplicate code every time the "return stage.sharedVar." and the "stage.sharedVar."+ the value + " = val" is constantly comming back.
i was wondering is there a other way of creating these get/sets?
something like?:
private function get variable1(){getValue("hisOwnFunctionName")}
private function set variable1(val){setValue("hisOwnFunctionName")}
and so on??
thanks,
Matthy
If I understand your question, you just want to get and set multiple properties on an Object. Depending on your situation you could try:
private function setProperty(name:Object,value:Object):void {
stage.sharedVar[name]=value;
}
private function getProperty(name:Object):Object {
return stage.sharedVar[name];
}
private function example():void {
setProperty("foo","bar");
trace(getProperty("foo")); //prints: bar
}
These functions will let you set and access the properties you want, and you wont have to keep changing the functions. It does mean that if you change superGrid to be something else, you can't just change the function, you have to change everywhere you use the get and setProperty. But it does mean that you don't have to keep writing new functions.
Hope this helps.
If you're in flex builder, download the web standard toolkit from http://download.eclipse.org/releases/galileo/ and use "snippets" to write these for you.
Otherwise, if you really need to do this within the program at run-time, you can make a wrapper proxy class to do all kinds of things: http://ltslashgt.com/2008/01/24/proxy-class-as3/