How do I reference WindowedApplication from inside a class I made? - apache-flex

I'm using Flex 4.5, and I have imported a custom class I wrote into the main MXML file.
Inside the class file, I want to be able to create a TitleWindow using the PopUpManager like this:
package classes {
import components.*; // My custom components
import mx.managers.PopUpManager;
public class SomeClass {
public function showPopUp():void {
PopUpManager.createPopUp(this,NewProjectPrompt,true);
}
}
}
NewProjectPrompt is a custom component I made. The compiler is giving me the following error:
1067: Implicit coercion of a value of type classes:Project to an unrelated type flash.display:DisplayObject.
This is because this isn't pointing at WindowedApplication. How do I make the first parameter in .createPopUp() point to the WindowedApplication?

this code works!
public function showPopUp(){
PopUpManager.createPopUp(FlexGlobals.topLevelApplication as DisplayObject,NewProjectPrompt,true);
}

If your WindowedApplication file is named "MyApp.mxml" then you would write a reference from a component to it like this:
MyApp(this.parentApplication)
This will return the actual WindowedApplication and you can call its public methods or stick it in a variable if need be.

Related

binding exists property in actionscript

how can i do binding exists property in actionscript e.g. i want image still in middle aplication..in mxml i do this simple as <mx:Image source="image.jpg" x="{this.width/2}"/> ...i don't know how can i do this simple in actionscript without event handlers... i put this code to application_creationCompleteHandler..
something like var image:Image = new Image(); image.source="image.jpg"; image.x=this.width/2; or have i put this to another function?? i can't do e.g. updateComplete event handler and change it there...
thanks
pavel
Check the BindingUtils class and its bindSetter method. You can bind a setter to the "width" property and adjust the x property of your image in that handler.
Considering that as3 is OO language i always start from something like this:
public class App extends Sprite {
public function App() { //constructor
super(); //extends class constructor
loading(); //your function were you set all elements properties
}
}

Flex Error on Simple Custom Event when Implemented in Actionscript (But not in MXML)

I'm trying to learn how to use custom events in Flex.
I'm following Oliver Merk's tutorial found here: blog
The custom event works if I implement it using MXML in the main app. But, if I use actionscript, then I get error 1119: Access of possibly undefined property ADD_PRODUCT through a reference with static type Class.
My Event:
In the events subdirectory, I've got:
package events {
   import flash.events.Event;
   public class AddProductEvent extends Event {
      public var productName:String;
      public function AddProductEvent( type:String, productName:String ) {
         super( type );
         this.productName = productName;
      }
      override public function clone():Event {
         return new AddProductEvent( type, productName );
      }
   }
}
In the component, I've got a radioButtonGroup
<mx:RadioButtonGroup id="choicesRadioButtonGroup" itemClick="onButtonClick()"/>
private function onButtonClick():void {
var myEventObj:Event = new AddProductEvent("addProduct", "Flex T-shirt");
dispatchEvent(myEventObj);
}
This is the metadata in the component and the import statement:
<mx:Metadata>
[Event (name="addProduct", type="events.AddProductEvent")]
</mx:Metadata>
import events.AddProductEvent;
In the main app, I've got:
import events.AddProductEvent;
private function onAddProduct( event:AddProductEvent ):void {
mx.controls.Alert.show('Attached data was ' + event.productName);
}
If I implement the component in the main app like this:
<visualcomponent:PopWindow addProduct="onAddProduct(event)" />
then everything works.
If I implement the component in the main app in actionscript like this, then I get an error:
public function clickHandler2(event:MouseEvent):void {
if(event.currentTarget.selected){popWindow = new PopWindow;
queryBuilder(event.currentTarget);
PopUpManager.addPopUp(popWindow, my_view, false);
PopUpManager.centerPopUp(popWindow);
popWindow.addEventListener(AddProductEvent.ADD_PRODUCT, onAddProduct);}
}
I get the error on the addEventListener line. What am I doing wrong? Any advice?
Thank you.
-Laxmidi
Your AddProductEvent class doesn't seem to expose a public static string called ADD_PRODUCT which has the value "addProduct" which is what it looks like you are trying to do.

create interface object in mxml

Let's say I have an interface
public interface IFoo {
...
}
and I have several implementing classes
public class Foo implements IFoo {
...
}
...
public class Bar implements IFoo {
...
}
...
public class Baz implements IFoo {
...
}
I want to reference IFoo in MXML like this
<ns:IFoo id="myfoo"/>
and have it be instantiated at runtime by a factory.
However, the compiler won't let me do this-- it's trying to do "new IFoo" in the generated ActionScript.
How to get around this? How can I use an interface and a factory purely in MXML?
Declaring an MXML child instantiates an object of that type - you can't simply declare a property in MXML without associating an instance with it.
Given that - there's no way to achieve the equivalent of
public var myFoo:IFoo;
in your MXML.
As James pointed out, you can use a ClassFactory to acheive the following:
<mx:ClassFactory class="{Foo}" id="fooFactory" />
but you would be required to call fooFactory.newInstance() to get an IFoo.
You can implement interfaces in MXML components with the implements="IFoo" attribute in the component's root node.
Edit:
Sorry, I missunderstood your question. I don't know a way to implement a factory in pure mxml. I guess you have to use Actionscript or mxml states to achieve a similar behaviour.
Check out ClassFactory. It is how things like item renderers are instantiated.

How do you access a stage instance name from a SWF embedded w/ the Flex compiler?

I've embedded a MovieClip symbol with the [Embed] syntax into my AS3 project, which I'm compiling with the Flex 3 SDK. That MovieClip has instances of other clips within it that are placed on stage with instance names. I can't just access them by instance name like I would if I were compiling with the Flash IDE.
How can I reference them?
you need to both give them instance names in the IDE and declare them in the class you've embedded them on.
So say that you have instances of baz and frr on your embedded class InfoPopup, you need to declare them like this:
package foo {
import flash.display.Sprite;
[Embed(source='../../../../../../assets/Assets.swf', symbol='InfoPopup')]
public class InfoPopup extends Sprite {
public var baz:Sprite;
public var baz:MovieClip;
public function InfoPopup(){
trace("constructor!");
}
}
}
When added like this they have to be public properties or else the compiler will complain.

What restrictions does public/private have in actionscript functions?

I'm currently maintaining some flex code and noticed very many functions which are declared like:
private function exampleFunc():void {
....
}
These functions are in the global scope, and aren't part of any specific class, so it's a bit unclear to me what effect declaring them as private would have. What restrictions does the "private" qualifier have for functions like this?
The actionscript functions that are included in your mxmlc code will we available as a part of your mxmlc component, which behind the scenes is compiled into a class. Therefore marking them as private makes them inaccessible.
Here is an example to make that clear, say you have the following component, we'll call it FooBox:
<!-- FooBox.mxml -->
<mx:Box xmlns:mx="http://www.macromedia.com/2003/mxml">
<mx:Script><![CDATA[
private function foo():void {
lbl.text = "foo";
}
public function bar():void {
lbl.text = "bar";
}
]]></mx:Sctipt>
<mx:Label id="lbl">
</mx:Box>
I can now add FooBox to my application, and use it's functions:
<mx:Application
xmlns:mx="http://www.macromedia.com/2003/mxml"
xmlns:cc="controls.*"
>
<mx:Script><![CDATA[
private function init():void {
fbox.foo(); // opps, this function is unaccessible.
fbox.bar(); // this is ok...
}
]]></mx:Sctipt>
<cc:FooBox id="fbox" />
</mx:Application>
If the actionscript functions are included in your Main Application file, the I think you can call the functions from an child control through the Application.application object, something like:
Application.application.bar();
if the bar function was placed in the main mxmlc code.
What do you mean by global scope? Are these functions declared in the main MXML file?
In general, private means that functions can only be called from within the class that declares them.
But, when you put it in a actionscript file .as is it still compliled into a class ?
Because asdoc doesn't like it.

Resources