im a newbie in flex. Im have a question :)
I have
[Bindable]
private var model:AlgorithmModel = new AlgorithmModel();
private var serviceProxy:Algorithm = new Algorithm( model );
In MXML
private function Show():void
{
// now model.Solve_SendResult = null
while(i<model.Solve_SendResult.length) //
{
Draw(); //draw cube
}
}
private function Solve_Click():void
{
//request is a array
Request[0] = 2;
Request[1] = 2;
Request[2] = 3;
serviceProxy.Solve_Send(request);
Show();
}
<s:Button x="386" y="477" label="Solve" click="Solve_Click();"/>
And when i call serviceProxy.Solve_Send(request); with request is array and i want use model.Solve_SendResult in my code flex to draw many cubes use papervison3d but in the first time i received model.Solve_SendResult = null . But when I click again then everything OK.
Anyone help me? Thanks?
The model.Solve_SendResult object contains a result of the executed serviceProxy.Solve_Send(request) method. The Solve_Send will be executed asynchronously and as a result, at the moment when you fire the show method the Solve_SendResult object may be still null.
As a solution, you can use the following:
Create a custom event
package foo
{
import flash.events.Event;
public class DrawEvent extends Event
{
public static const DATA_CHANGED:String = "dataChanged";
public function DrawEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
}
}
}
In your Algorithm class define the following:
[Event(name=DrawEvent.DATA_CHANGED, type="foo.DrawEvent")]
public class Algorithm extends EventDispatcher{
//your code
In the Solve_SendHandler method of the Algorithm class add the following
public virtual function Solve_SendHandler(event:ResultEvent):void
{
dispatchEvent(new DrawEvent(DrawEvent.DATA_CHANGED));
//your code
}
In your MXML class create onLoad method and add an event listener to an instance of the Algorithm class as it shown below:
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="onLoad()">
public function onLoad():void
{
serviceProxy.addEventListener(DrawEvent.DATA_CHANGED, onDataChanged);
}
private function onDataChanged(event:DrawEvent):void{
while(i<model.Solve_SendResult.length) //
{
Draw(); //draw cube
}
}
make the following changes in the Solve_Click() method:
private function Solve_Click():void
{
//request is a array
Request[0] = 2;
Request[1] = 2;
Request[2] = 3;
serviceProxy.Solve_Send(request);
}
That is it! So, basically the code above do the following: you added a listener to your service (algorithm class), and the listener is listening for the DrawEvent.DATA_CHANGED event. The DrawEvent.DATA_CHANGED will be dispatched when your client receive a result of the Solve_Send invocation. Thus, the onDataChanged will draw your cube or do whatever you want :)
The approach above is basic and you have to know how events work in flex and how you can deal with it. Additional information is available here:
http://livedocs.adobe.com/flex/3/html/help.html?content=createevents_3.html
http://livedocs.adobe.com/flex/3/html/help.html?content=events_07.html
Regards,
Cyril
Related
I want to capture a change in a property of an item as follows
myItem.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE,listener);
protected function listener(event:PropertyChangeEvent):void {
...
}
the problem i'm having is that when I assign multiple values to the "myItem" object the listened gets kicked off multiple times. for instance
If I do:
myItem.x = new_x;
myItem.y = new_y;
....
the listener kicks off everytime a change happens (after calling first line, then after calling second line..etc). How to prevent that to save processing/memory and avoid inconsistency.
You can listen for Event.COMPLETE (or create a custom event) and manually dispatch that when you've finished changing all your properties. For example:
myItem.addEventListener(Event.COMPLETE, listener);
protected function listener(e:Event):void
{
...
}
then
myItem.x = newX;
myItem.y = newY;
myItem.dispatchEvent(new Event(Event.COMPLETE));
You can override the setter of your component and dispatch a custom event
You can use mx.utils.ObjectProxy class and by overriding its protected "setupPropertyList" method you can specify by youself what properties will trigger PropertyChangeEvent.PROPERTY_CHANGE event
You could do that the "hackish" way. Example follows...
BindingObject.as:
package bindings
{
import mx.core.EventPriority;
import mx.events.PropertyChangeEvent;
import mx.events.PropertyChangeEventKind;
[Bindable]
public class BindingObject
{
private var inEditMode : Boolean = false;
public function BindingObject()
{
this.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, onPropertyChange, false, EventPriority.BINDING - 1);
}
private function onPropertyChange(event : PropertyChangeEvent) : void
{
if (inEditMode)
event.stopImmediatePropagation();
}
public function beginEdit() : void
{
inEditMode = true;
}
public function endEdit() : void
{
inEditMode = false;
this.dispatchEvent(new PropertyChangeEvent(PropertyChangeEvent.PROPERTY_CHANGE, false, false, PropertyChangeEventKind.UPDATE));
}
public var myX : int = 0;
public var myY : int = 0;
}
}
testapplication.mxml:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="onCreationComplete();">
<mx:Script>
<![CDATA[
import mx.events.PropertyChangeEvent;
import bindings.BindingObject;
[Bindable]
private var something : BindingObject = new BindingObject();
private function onCreationComplete() : void
{
something.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, listener);
something.beginEdit();
trace("Begun edit");
trace("changing myX");
something.myX = 3;
trace("changed myX");
trace("changing myY");
something.myY = 6;
trace("changed myY");
trace("Ending edit");
something.endEdit();
trace("Ended");
}
private function listener(event : PropertyChangeEvent) : void
{
trace("in my listener");
}
private function myX(val : int) : String
{
trace("in myX");
return val.toString();
}
private function myY(val : int) : String
{
trace("in myY");
return val.toString();
}
]]>
</mx:Script>
<mx:Label text="{myX(something.myX)}" />
<mx:Label text="{myY(something.myY)}" />
</mx:Application>
So, as you can see, I've added an event listener in the class to be bound, that will be triggered right after the generated binding listeners (note the priority is BINDING - 1) and in there I stop the propagation of the event. Which means whatever listeners still need to be executed, won't. The endEdit method will dispatch the PropertyChange event that will trigger your listener.
Now, assuming that you do something costly in your listener this should solve your problem.
I'm trying to set the height of a vertical bar (activityBar) but it does not appear to do anything. i have tried something similar with the whole component, but setting the dimensions does nothing (even in the mxml used to instantiate the class). Indeed, I've added transparent graphics just to give the component some dimensions
I'm not sure what I'm doing wrong. It's something bad though; my approach seems dire.
FYI: I'm trying to create a mic activity bar that will respond to the mic by simply setting the height of the activityBar child (which seems to me to be more efficient than redrawing the graphics each time).
Thanks for your help!
package components {
import mx.core.UIComponent;
public class MicActivityBar extends UIComponent {
public var activityBar:UIComponent;
// Constructor
public function MicActivityBar() {
super();
this.opaqueBackground = 0xcc4444;
graphics.beginFill(0xcccccc, 0);
graphics.drawRect(0,-15,5,30);
graphics.endFill();// background for bar
activityBar = new UIComponent();
activityBar.graphics.beginFill(0xcccccc, 0.8);
activityBar.graphics.drawRect(0,-15,5,20);
activityBar.graphics.endFill();
activityBar.height=10;
addChild(activityBar);
}
}
}
package components {
import mx.core.UIComponent;
public class MicActivityBar extends UIComponent {
public var activityBar:UIComponent;
private var _w:Number;
// Constructor
public function MicActivityBar() {
super();
this.opaqueBackground = 0xcc4444;
graphics.beginFill(0xcccccc, 0);
graphics.drawRect(0,-15,5,30);
graphics.endFill();// background for bar
activityBar = new UIComponent();
activityBar.graphics.beginFill(0xcccccc, 0.8);
activityBar.graphics.drawRect(0,-15,5,20);
activityBar.graphics.endFill();
activityBar.height=10;
addChild(activityBar);
}
public function get w():Number {
return _w;
}
public function set w(value:Number):void {
_w = value;
}
override protected function updateDisplayList(unscaledWidth:Number,unscaledHeight:Number):void {
super.updateDisplayList(unscaledWidth, unscaledHeight);
this.height=w;
this.weight=w;
}
}
}
See my updated code above.
I have small chess application which consists of cells and boards. When user moves an item to the board, I want the board cell to dispatch an event so Board can listen to it and call a listener
public class BoardCell extends Canvas
{
public function Sample():void
{
....Some code
var e:Event = new Event("newMove")
dispatchEvent(e);
}
}
However, I can't catch the event in parent chess board class (Not sure that I listen for it correctly)
public class FrontEndBoard extends ChessBoard
{
private var initialPoition:String;
public function FrontEndBoard()
{
//TODO: implement function
this.addEventListener(Event.ADDED_TO_STAGE, addedToStage);
this.addEventListener("newMove", moveEvent);
super();
}
you have 2 options :
1) instead of this.addEventListener("newMove", moveEvent); do BoardCell.addEventListener("newMove", moveEvent);
2) have the event buble up to the parent ( assuming BoardCell is a display child of FrontEndBoard , you set it as a parameter in the event constructor )
var e:Event = new Event("newMove",true) .
I'm not sure how exactly FrontEndBoard and BoardCell are hierarchically in your application, but you may need to tell the "newMove" event that it can bubble.
var e:Event = new Event("newMove", true);
The event you dispatch from the BoardCell class should bubble, so it is caught in any parent classes. Check the constructor arguments of the Event class where you can set the "bubbles" flag to true.
I would like to track the customer experience in downloading and initializing my flex app.
(a) Is there a way to pass data from preloader to the application? I would like to pass the time it takes to download and the time it takes to initialize.
(b)Alternatively:
Is there an event at the application level that corresponds to the preloader events:
1. Download complete
2. Initialization complete (same as Application creationComplete)
The "Showing the download progress of an application" article in the livedocs should help.
Based on that documentation, I would do something like this:
create a simple subclass of the DownloadProgressBar,
override the event listeners to track the amount of time that has elapsed during download/initialisation,
store the time values as static properties so you can access them from your Application once it's completed initialisation.
Here is an example of what I'm thinking (I've not compiled this code, it's more to give an idea of what I'm talking about).
package
{
public class TimedProgressBar extends mx.preloaders.DownloadProgressBar
{
public static var startTime:Number = 0;
public static var downloadCompleteTime:Number = 0;
public static var RSLCompleteTime:Number = 0;
public function TimedProgressBar()
{
super();
startTime = getTimer();
}
override protected function completeHandler(event:Event):void
{
super();
downloadCompleteTime = getTimer();
}
override protected function rslCompleteHandler(event:RSLEvent):void
{
super();
RSLCompleteTime = getTimer();
}
}
}
Set that as your preloader in your Application.mxml and listen for the APPLICATION_COMPLETE event:
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
preloader="TimedProgressBar"
applicationComplete="applicationCompleteHandler(event)">
private function applicationCompleteHandler(event:FlexEvent):void
{
var completeTime:Number = getTimer();
var downloadTime:Number = TimedProgressBar.downloadCompleteTime - TimedProgressBar.startTime;
var rslDownloadTime:Number = TimedProgressBar.RSLCompleteTime - TimedProgressBar.downloadCompleteTime;
var totalInitTime:Number = completeTime - TimedProgressBar.startTime;
// Do whatever logging you want with this information.
}
When I debug my code nothing appears on screen. I've rechecked the code and consulted with others yet nothing appears. My html template is fine.
package {
import flash.display.Sprite;
import flash.events.*;
public class asgnv2 extends Sprite
{
var lineY = 0;
public function asgnv2()
{
stage.addEventListener(Event.ENTER_FRAME, update);
graphics.lineStyle(1);
}
function update(e){
graphics.clear();
graphics.moveTo(0 ,lineY);
graphics.lineTo(100, lineY);
lineY+=0.5;
}
}
}
unless asgnv2 is Document class, it is not going to work, as you are registering ENTER_FRAME event on the stage inside the constructor of asgnv2. A DisplayObject can not access stage property until it is added to Stage Display List. So try the following. public function asgnv2(){
this.addEventListener(Event.ADDED_TO_STAGE, onAdded);
graphics.lineStyle(1);
}
private function onAdded(e:Event):void {
stage.addEventListener(Event.ENTER_FRAME, update);
this.removeEventListener(Event.ADDED_TO_STAGE, onAdded);
}
private function update(e:Event):void{
//do the stuff
}