I need to pass a variable in dispatcher tag... for instance
var google:EventName = new EventName(EventName.ADD_User,user);
dispatchEvent(google);
Now when i go to Mate dispatcher tag... how can i pass the value user.
<mate:Dispatcher id="myDispatcher" generator="{EventName}"
type="{EventName.ADD_User}">
<mate:eventProperties>
<mate:EventProperties
myProperty="myValue"
myProperty2="100" />
</mate:eventProperties>
</mate:Dispatcher>
Now how can i pass the user in the mate dispatcher tag.
You would have to declare user as a property of your GoogleEvent.as class. For GoogleEvent.as:
package
{
public class GoogleEvent extends Event
{
public static const ADD_User:String = "GoogleEvent_AddUser";
public var user:String;
public function GoogleEvent(type:String, bubbles:Boolean=true, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
}
}
}
And in your event declaration:
var google:GoogleEvent= new GoogleEvent(GoogleEvent.ADD_User);
google.user = "My User's Name";
dispatchEvent(google);
Then to get the user value from your event in your Event Map:
<EventHandlers type="{ GoogleEvent.ADD_User }">
<MethodInvoker generator="{ MyTargetPresentaionModel }" method="DoSomething" arguments="{ [event.user] }" />
</EventHandlers>
Let me know if you have any questions. I hope this helps!
Related
When writing methods for Action Parameters in Caliburn.Micro:
If ActionExecutionContext is the class you use to pass $executionContext, and if KeyEventArgs is the claas you use to pass $eventArgs, then what class do you use to pass $source?
If you know the class of the $source, you could use directly it, either you could use object and test casting.. See a sample with DependencyObject
<Button Content="SelectAll" cal:Message.Attach="[Event Click] = [Action SelectAll($source)]" />
public void SelectAll(object sender)
{
if (sender is DependencyObject)
//something to do
//or
var Grid = (DependencyObject)sender);
}
if you know the class sended:
public void SelectAll(DependencyObject sender)
{
}
I want to change Property Of Custom Component Popup From Main.mxml (Main Application)
My Custom Component is ->
public class PropertyPanel extends Panel
.....
in it I have
public function minimisePanel(e:MouseEvent):void{
effResize.heightTo = previousHeight;
effResize.widthTo = 200;
this.x = parentApplication.width - 320;
effResize.play([this]);
}
In Main Application I Called It As -->
private function AddPropertiesPanel():void{
var PropWindow:IFlexDisplayObject;
PropWindow = PopUpManager.createPopUp(this, Property_Panel, false);
/*Property_Panel is Property_Panel.mxml*/
}
And In Main Application I Want to -->
public function setCurrObj(event:TransformEvent):void{
/*Some Magical Stuff Required Here*/
}
Instead of storing the popup as a variable local to the function; store it as an instance variable on the Main.mxml class:
public var PropWindow:IFlexDisplayObject;
The AddPropertiesPanel() method will change like this:
private function AddPropertiesPanel():void{
PropWindow = PopUpManager.createPopUp(this, Property_Panel, false);
/*Property_Panel is Property_Panel.mxml*/
}
And then you can easily access properties on the panel instance in other methods:
public function setCurrObj(event:TransformEvent):void{
PropWindow.someProperty = someValue
}
Below is my class, which simply reads an xml file and provides the contents in e4x format. Unfortunately, after the constructors executes and sets the xmlProperties property with the expected values, it some how becomes null. Anyone know what I'm doing wrong?
public class WebService
{
private var _propertiesReader:HTTPService;
private var _xmlProperties:XML;
public function WebService()
{
_propertiesReader = new HTTPService();
_propertiesReader.url = "../resources/properties.xml";
_propertiesReader.resultFormat = "e4x";
_propertiesReader.contentType = "application/xml";
_propertiesReader.addEventListener(ResultEvent.RESULT, function(event:ResultEvent):void
{
_xmlProperties = XML(event.result);
});
_propertiesReader.addEventListener(FaultEvent.FAULT, function(event:FaultEvent):void
{
Alert.show("Unable to load properties content: " + event.fault.message + "\nPlease try again later.", "Properties File Load Error");
});
_propertiesReader.send();
}
public function get xmlProperties():XML
{
return _xmlProperties;
}
}
_xmlProperties is being set by a File Load call (via a callback event). It is not being set directly in the constructor.
Are you sure you are waiting for the call to finish and the callback event to fire before you check the value of _xmlProperty?
I am building a AS3 only project and got runtime error that said "Cannot access a property or method of a null object reference."
Here is my code:
main.as
public class videoMain extends Sprite{
private var videoPlayer:Player;
public function videoMain (){
loadPlayer()
loadProgress();
}
private function loadProgress():void{
//the code below gave me null object error.....
var byteLoaded:Number=videoPlayer.videoBytesLoaded; //the problem code
var byteTotal:Number=videoPlayer.videoBytesTotal; //the problem code
var percent:Number=Math.floor(byteLoaded/byteTotal)*100;
}
private function loadPlayer():void{
videoPlayer= new Player();
videoPlayer.createPlayer();
}
}
Player.as
public function createPlayer():void{
_loader = new Loader();
_loader.contentLoaderInfo.addEventListener(Event.INIT, onLoaderInit);
_loader.load(new URLRequest(playerType));
}
public function get videoBytesLoaded():Number{
return _Player.getVideoBytesLoaded(); //youtube api method
}
public function get videoBytesTotal():Number{
return _Player.getVideoBytesTotal; //youtube api method
}
private function onLoaderInit(event:Event):void {
_Player=_loader.content;
//only show part of codes....
}
I appreciate any help....Thanks!!!!!
_Player is only defined after the Event.INIT has fired so any call before the _Player value is defined will throw an error.
You should, at the minimum , have this:
public function videoMain (){
loadPlayer()
}
private function onLoaderInit(event:Event):void {
_Player=_loader.content;
//only show part of codes....
loadProgress();
}
but progress events are not static so really you should have an enterFrame event listener in order to listen to the changing values...
private function onLoaderInit(event:Event):void {
_Player=_loader.content;
//only show part of codes....
addEventListener(Event.ENTER_FRAME , enterFrameListener);
}
private function enterFrameListener(event:Event):void
{
loadProgress();
// and here you add some way to remove this event listener when
// the video is fully loaded
}
in main.mxml :
public function init():void
{
PopUpManager.createPopUp(this,login,true);
}
<mx:ViewStack id="vs" label="content" >
<local:FrontPanel id="Fpanel" />
<local:SlavePanel id="Spanel" />
<local:AdminPanel id="Mpanel" />
</mx:ViewStack>
In Login.mxml :
public function authenticateRH(event:ResultEvent):void
{
var replyMsg:String=event.result as String;
switch(replyMsg)
{
case "Master" :
here i want invoke Mpanel from Main.mxml
break;
case "Slave" :
here i want invoke Spanel from Main.mxml
break;
case "fail" :
Alert.show("Login Incorrect!!");
return;
}
}
How can i selectchild of main.mxml's Viewstack from login.mxml ?
I would use events (and interfaces), that way your forms don't need to know as much about one and other. It also gives you more control on what happens after your popup closes (IMHO).
Here is a little example (if I can get the code to paste correctly). This example does not use an interface that would help decouple things. I also use dynamic events that make prototyping faster but you will want to define a formal event.
simulate your main:
import mx.events.DynamicEvent;
import mx.managers.PopUpManager;
private function btnClick(e:Event):void
{
vs.selectedIndex = 0;
var myLogin:myPopup = new myPopup();
myLogin.addEventListener
(
"WAS_CLOSED",
function(e:DynamicEvent):void { vs.selectedIndex = e.byButton; }
);
PopUpManager.addPopUp(myLogin, this, true);
PopUpManager.centerPopUp(myLogin);
}
simulate your login:
import mx.events.DynamicEvent;
import mx.managers.PopUpManager;
private var eClose:DynamicEvent = new DynamicEvent("WAS_CLOSED");
private function closeMe(byButton:int):void
{
eClose.byButton = byButton;
dispatchEvent(eClose);
PopUpManager.removePopUp(this);
}
<mx:Button x="122" y="250" label="Thing 1" click="closeMe(1);" />
<mx:Button x="195" y="250" label="Thing 2" click="closeMe(2);" />
public function init():void
{
var popup:IFlexDisplayObject = PopUpManager.createPopUp(this, Login, true);
popup.addEventListener(Event.SELECT, onSelect);
}
private function onSelect(e:CustomEvent):void
{
if(e.item == CustomEvent.MASTER)
{
vs.selectedItem = Mpanel;
}
else if(e.item == CustomEvent.SLAVE)
{
vs.selectedItem = Spanel;
}
//remove popup
PopUpManager.removePopUp(IFlexDisplayObject(e.target));
}
//Login.mxml :
public function authenticateRH(event:ResultEvent):void
{
var replyMsg:String=event.result as String;
switch(replyMsg)
{
case "Master" :
dispatchEvent(new CustomEvent(CustomEvent.MASTER));
break;
case "Slave" :
dispatchEvent(new CustomEvent(CustomEvent.MASTER));
break;
case "fail" :
//remove popup
PopUpManager.removePopUp(this);
Alert.show("Login Incorrect!!");
return;
}
}
//CustomEvent.as
public class CustomEvent extends Event
{
public static const MASTER:String = "master";
public static const SLAVE:String = "slave";
public var item:String;
public function CustomEvent(item:String)
{
super(Event.SELECT);
this.item = item;
}
}
I'm assuming your problem is that you don't have a reference to your ViewStack while in your authenticateRH method. One way you can fix this is to give your login popup some more information after you create it:
public function init():void
{
var loginPopup:login = PopUpManager.createPopUp(this,login,true) as login;
loginPopup.setUsefulInformation(referenceToViewStack);
}
You'd have to add the setUsefulInformation() method to login.mxml. It's up to you what information you want to pass to solve the problem.
first, i believe you're creating your pop-up incorrectly. you seem to have the first two parameters switched. should be:
PopUpManager.createPopUp(login,this,true);
docs: http://livedocs.adobe.com/flex/3/langref/mx/managers/PopUpManager.html#addPopUp%28%29
second, you need to either explicitly give your Login class a reference to your Main class or if your Login class is on the same display chain as your Main class you can use a custom event with Bubbles set to true.