czwartek, 23 lutego 2012

How to know when item in ItemRenderer was selected

ItemRenderer doesn't have event itself that let us know that current ItemRenderer was selected (only owner - List have change event)

Easy way to handle change state is to override selected setter:

override public function set selected(v:Boolean):void
{
    var oldValue:Boolean = selected;
   
    super.selected = v;
   
    if(oldValue != selected)
    {
        trace("change");

        if(selected)
        {
            trace("selected");
        }else{
            trace("deselected");
        }
    }
}

piątek, 17 lutego 2012

Never use boolean values in php define function

Last time I hade trouble with my defined global variable in php. I used simple code:

define("MY_VARIABLE", true);

and then tried to check if it was defined to true, so:

if(MY_VARIABLE == true)
{
    echo 'true';
}

It was ok... until i didn't defined it. Condition still was correct:

// When we remove this line: define("MY_VARIABLE", true);

if(MY_VARIABLE == true)
{
    echo 'true';
}

result is still 'true'. We just get notice, but most of php compilations have notices disabled.

Better way is to use string and compare it:

define("MY_VARIABLE", 'true');
if(MY_VARIABLE == 'true')
{
    echo 'true';
}

Or use strict condition with tripple =

if(MY_VARIABLE === true)

Now everything will be ok.

How to copy ArrayCollection in Flex

Way to make a copy of ArrayCollection is very simple, we just need to create a new instance of ArrayCollection and pass array to constructor:


var ac1:ArrayCollection = new ArrayCollection(["a", "b", "c", "d"]);
trace("ac1: " + ac1);

var ac2:ArrayCollection = new ArrayCollection(ac1.toArray());
trace("ac2: " + ac2);

ac2.setItemAt("X", 2);
               
trace("ac1: " + ac1);
trace("ac2: " + ac2);

The result of this code will be:

ac1: a,b,c,d
ac2: a,b,c,d
ac1: a,b,c,d
ac2: a,b,X,d

So as You can see, ac1 wasn't modified. I also tried this with class objects, insted of simple string or number.

piątek, 27 stycznia 2012

Always make clone method in robotlegs events

I just spend some time looking what is wrong when I was calling my custom robotlegs Command.

Error: Injector is missing a rule to handle injection into property "event" of object...

It turned out that I don't have clone method in event that triggered command.
So remember to always override clone method.