0
votes

I am doing a project in Flash Builder using ActionScript.

I have two MXML files: login.mxml and welcome.mxml.

login.mxml:

var username:String="sample";
var password:String="sample";

welcome.mxml:

trace("welcome"+username); // o/p-welcome sample

I have to pass the username value from login.mxml to welcome.xml. Is it possible to pass a variable value from one MXML to another MXML file? How?

1

1 Answers

0
votes

Yes, it is possible. The best way though would be bind the views to an objects values.

Your views don't seem to be to be associated in terms of "one aggregates the other" but their parent (the container view) knows both. So the parent would pass an object reference to both views and when this instance is updated, the views will be notified and and updated via data binding.

If the views are completely independent from each other, it would be the most straight forward way to dispatch events through the application via the application. You should introduce a new event type (i.e. SystemEvent) which is dispatched by the Application. To keep you application clean from too many references to specific global variables used in the views, i'd suggest a delegate if you're to firm with MVC yet:

package de.guj.vila.delegates {
  import flash.events.Event;
  import flash.events.IEventDispatcher;

  import mx.core.FlexGlobals;

  import mx.core.IMXMLObject;
  import mx.core.UIComponent;

  public class ViewDelegate implements IEventDispatcher, IMXMLObject {

    //---------------------------------------------------------------------
    //
    //          Properties
    //
    //---------------------------------------------------------------------

    private var _bus:IEventDispatcher;

    private var _uiComponent:UIComponent;

    /**
     * The view which uses the delegate.
     */
    public function set uiComponent(value:UIComponent):void {
      _uiComponent = value;
    }

    //---------------------------------------------------------------------
    //
    //          Constructor
    //
    //---------------------------------------------------------------------

    public function ViewDelegate() {
      _bus = FlexGlobals.topLevelApplication as IEventDispatcher;
    }

    //---------------------------------------------------------------------
    //
    //          Implemented Methods
    //
    //---------------------------------------------------------------------

    /**
     * @inheritDoc
     *
     * @see flash.events.IEventDispatcher
     */
    public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void {
      _bus.addEventListener(type, listener, useCapture, priority, useWeakReference);
    }

    /**
     * @inheritDoc

     * @see flash.events.IEventDispatcher
     */
    public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void {
      _bus.removeEventListener(type, listener, useCapture);
    }

    /**
     * @inheritDoc

     * @see flash.events.IEventDispatcher
     */
    public function dispatchEvent(event:Event):Boolean {
      return _bus.dispatchEvent(event);
    }

    /**
     * @inheritDoc
     *
     * @see flash.events.IEventDispatcher
     */
    public function hasEventListener(type:String):Boolean {
      return _bus.hasEventListener(type);
    }

    /**
     * @inheritDoc
     *
     * @see mx.core.IMXMLObject
     */
    public function initialized(document:Object, id:String):void {
      uiComponent = document as UIComponent;
    }

    /**
     * @inheritDoc
     *
     * @see flash.events.IEventDispatcher
     */
    public function willTrigger(type:String):Boolean {
      return _bus.willTrigger(type);
    }
  }
}

You can just stuff the in the fx:Declarations block, give it an id and dispatch events from view to view. You just have to set up the listeners. This way you can easily implement quite a clean structure, since you just have to refactor the delegate. Utilizing the delegate as a base class, you can event handle any events in the delegate, so you views stay clean and, most importantly, it's easy to port to a different MVC approach, because you already isolated view behaviour from the applications behaviour.

In the end you'll want to use an MVC framework (RobotLegs for example) to be able to scale your application easily.