0
votes

I have a singleton class that looks something roughly like this (only with more bindable public properties):

public class Session extends EventDispatcher
{
    private var _Id:String;
    private static const _instance:Session = new Session( SingletonLock );
    private static const SESSID_CHANGED:String = 'SessionIdChanged';


    public function Session( lock:Class ){
    //SingletonLock is an empty class not available outside this file
        if( lock != SingletonLock ){
            throw new Error("Don't instantiate Session. Use Session.instance");
        }
        _Id = "";

    }

    public static function get instance():Session{
        return _instance;
    }

    // Changes a blob object (from the server xml for sessions) to a session object
    public function updateFromXMLObj(s:ObjectProxy):void
    {
        _instance.Id = s.Id;
    }

    [Bindable(event=SESSID_CHANGED)]
    public function get Id():String{
        return _Id;
    }

    public function set Id(new_id:String):void{
        if(this._Id != new_id){
            this._Id = new_id;
            this.dispatchEvent(new Event(SESSID_CHANGED));
        }
    }

    public function registerOnSessionChange(listener:Function):void{
        addEventListener(SESSID_CHANGED,listener);
    }

    public function unregisterOnSessionChange(listener:Function):void{
        removeEventListener(SESSID_CHANGED,listener);
    }
}

The idea is that in some mxml code, I have a databinding expression like the following:

<mx:HTTPService id="homeReq" url="{URLs.homepath(Session.instance.Id)}" ... />

where I want the url for homeReq to be updated when the sessionId changes. In addition, other parts of the code (written in Actionscript) need to be able to register their listeners for when the sessionId changes, so they call registerOnSessionChange and unregisterOnSessionChange to manage those listeners.

The abnormal behavior I'm discovering is that the event listeners registered through registerOnSessionChange are indeed being called when the session Id changes, but the MXML data binding expression is not updating. I've tried all combinations of dispatching the event during the capture phase, and making it not cancelable, but to no avail. My understanding of [Bindable (event= ...)] is that the MXML should update the url string when the event specified is dispatched, so what am I doing wrong or misunderstanding?

Note: I realize there are lots of different ways of doing the singleton pattern in Actionscript, but unless the way I am doing it is actually causing my problem somehow, I'd appreciate not getting sidetracked by discussing alternatives.

2

2 Answers

0
votes

I think that {URLs.homepath(Session.instance.Id)} this is not binding to a variable instead is executing a method of an object, have you tried to do something like this:

[Bindable]
private var _url:*

Then setting the initial value to _url at init or complete:

_url = {URLs.homepath(Session.instance.Id)};

Linking to the binded variable in the MXML

<mx:HTTPService id="homeReq" url="{_url}" ... />

Then updating the _url variable should automatically update the HTTPService url...

0
votes
  1. Make an MXML form containing a combobox for course number of 5th semester. On selecting the coruse, display the course name and max marks for the selected course. Data Binding: <mx:Binding>