开发者

How to make a property bindable?

I have a class like this. Property "isPag" is based on filed "ecboardid", I found that when ecboardid is changed, UI controls seem not be able to detect that "isPag" is also changed. So, how to make a property like this bindable?

[Bindable]
public class Encoder extends EventDispatcher
{
    public var ecboardid : String;

   /*-.........................................Methods..........................................*/
    public function copyFrom(newEncoder:Encoder):void 
    {
        ecboardid = newEncoder.ecboardid;
        this.dispatchEvent(new Event('isPagChanged'));
    }

    [Bindable (event="isPagChanged")]
    public function get isPag():Boolean
    {
        if(this.ecboardid != null)
        {
            if(this.ecboardid.search('xxx') != -1)
            {
                return false;
            }

            return true;                
        }
        else
        {
            return false;
        }
    }
}

Edit:

If I change the property to a static function,

[Bindable]
public class Encoder extends EventDispatcher
{
    public var ecboardid : String;

   /*-.........................................Methods..........................................*/
    public function copyFrom(newEncoder:Encoder):void 
    {
        ecboardid = newEncoder.ecboardid;
        this.dispatchEvent(new Event('isPagChanged'));
    }

    public static function isPag(String ecboardid ):Boolean
    {
        if(ecboardid != null)
        {
            if(ecboardid.search('xxx') != -1)
            {
                return false;
            }

            return true;                
        }
        else开发者_开发知识库
        {
            return false;
        }
    }
}

Will bind like this :

visible = {Encoder.isPag(encoder.ecboardid)}

work? Will visible change when encoder.ecboardid change?


I don't believe you can make read only properties Bindable, since Flex uses calls to the setter method for a property to issue the change events. Take a look here at the section on binding to properties of objects.


You need to have a setter as well as a getter.

Also, you have the class marked as bindable, if you weren't specifying the event name, then the [Bindable] on the method would be redundant.

Usually the compiler complains if you have just a getter and no setter and try to mark it [Bindable]

Code like this:

visible = {Encoder.isPag(encoder.ecboardid)}

Isn't how binding works. The {var} notation is for use in MXML. And isPag isn't static, so you can't refer to it as a class property. You need an instance of the Encoder class for this to be legal.


This is a simple change - just create getters/setters and dispatch your change event in the setter (after changing the value), like this:

    private var _ecboardid : String;


    public function get ecboardid():String
    {
        return _ecboardid;
    }

    public function set ecboardid(value:String):void
    {
        _ecboardid = value;
        this.dispatchEvent(new Event('isPagChanged'));      
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜