Dynamic tree in Flex - apache-flex

i wanna make a dynamic tree using a lazy loading ,each time i open a folder the tree sends a http request to the server, in this script i'm using just static text to test the tree but , i'm getting in the label of the root all the XML text assigned to the dataprovider, then when i open the root folder i got the childs with good labels , and openitem and closeitem events do not fire how could i make them work , any help is welcome
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
import mx.collections.XMLListCollection;
import mx.events.ListEvent;
var origXML:XML;
public function initList()
{
//tree.rootVisible = false;
//TODO: Get this XML from a data service
var origXMLString:String = "<node isBranch=\"true\"><node>supernode1</node>" +
"<node>supernode2</node>" +
//"<node label=\"supernode2\" someProp=\"sdsdf \" isBranch=\"true\"/></node>" +
//"<node label=\"supernode3\" someProp=\"sdsdf \" isBranch=\"true\"/></node>" +
"</node>";
origXML = new XML(origXMLString);
tree.dataProvider = origXML;
}
public function open(event:Object)
{
var selectedNode:Object = event.node;
var myXMLList:XMLList = new XMLList(selectedNode);
//TODO: Get this XML from a data service based on the selected node.
var newXMLString:String = "<childnode1 label=\"childnode1\" someProp=\"sdsdf \" isBranch=\"true\" />" +
"<childnode2 label=\"childnode2\" someProp=\"sdsdf \" isBranch=\"false\" />" +
"<childnode3 label=\"childnode3\" someProp=\"sdsdf \" isBranch=\"true\" />" ;
var myNewXMLList:XMLList = new XMLList(newXMLString);
selectedNode.setChildren(myNewXMLList);
/* myText1.text = selectedNode.toXMLString();
myText2.text = myTree.dataProvider.source[0]; */
tree.dataProvider = origXML;
}
public function close(event:Object)
{
var selectedNode:Object = event.node;
var myXMLList:XMLList = new XMLList(selectedNode);
removeAllDecendants(myXMLList);
/* myText1.text = selectedNode.toXMLString();
myText2.text = myTree.dataProvider.source[0]; */
tree.dataProvider = origXML;
}
public function removeAllDecendants(xmlList:XMLList)
{
var myDescendantXMLList:XMLList = xmlList.descendants();
var myDecendentXMLListCollection:XMLListCollection = new XMLListCollection(myDescendantXMLList);
myDecendentXMLListCollection.removeAll();
}
private function send_data():void {
var loader : URLLoader = new URLLoader();
var request : URLRequest = new URLRequest("http://localhost/index.php" );
// pass the post data
request.method = URLRequestMethod.POST;
var variables : URLVariables = new URLVariables();
variables.s = "haha";
request.data = variables;
// add handlers
loader.addEventListener(Event.COMPLETE, on_complete);
loader.load(request);
// userRequest.send();
}
private function on_complete(e : Event):void{
}
]]>
</fx:Script>
<mx:Tree id="tree" x="103" y="49" width="445" height="278" enabled="true"
itemClose="close(event)" itemOpen="open(event)" selectedIndex="1"></mx:Tree>

Here is a basic one http://flexdiary.blogspot.com/2009/01/lazy-loading-tree-example-file-posted.html
Here is a Mate one
http://www.developria.com/2010/05/refactoring-with-mate.html
Here is a Robotlegs one
http://flexdiary.magnoliamultimedia.com/RobotLegsHierarchicalRemoteObject/RobotLegsHierarchicalRemoteObject.html

Related

Passing Variables to ASPX to the URL String from AS3 - Message Failed

Im trying to send three variables (fname, lname, and email) to append them to the URL string like this http://www.whatever.com?address=&firstname=&lastname=&email= and it traced as "Message Failed" I wonder what I did wrong with this code.
in Flash 'fname_txt', 'lname_txt', and 'email_txt' are the instance names of the Input Text
import flash.events.MouseEvent;
import flash.net.URLRequest;
import flash.net.navigateToURL;
mcButton.addEventListener(MouseEvent.MOUSE_UP, onClick);
function onClick(e:MouseEvent):void {
var scriptRequest:URLRequest = new URLRequest("../index.aspx");
var scriptLoader:URLLoader = new URLLoader();
var scriptVars:URLVariables = new URLVariables();
scriptLoader.addEventListener(Event.COMPLETE, handleLoadSuccessful);
scriptLoader.addEventListener(IOErrorEvent.IO_ERROR, handleLoadError);
scriptVars.fname = fname_txt.text;
scriptVars.lname = lname_txt.text;
scriptVars.email = email_txt.text;
scriptRequest.method = URLRequestMethod.POST;
scriptRequest.data = scriptVars;
scriptLoader.load(scriptRequest);
function handleLoadSuccessful($evt:Event):void
{
trace("Message sent.");
}
function handleLoadError($evt:IOErrorEvent):void
{
trace($evt); <----------------UPDATED
}
fname_txt.text = "";
lname_txt.text = "";
email_txt.text = "";
}

actionscript to populate a list from sqlite table

using Adobe Flash Builder 4.6
the below code is what I am using to try to get actionscript to populate a list a from sqlite table. It brings back the correct number of records but it shows the results as:
[object Object]
[object Object]
[object Object]
Can someone tell me what I may be doing wrong?
private var strGetDBName:String = "CPRInstr.db";
private var strGetCurrentTableName:String = "lkStates";
import flash.data.SQLConnection;
import flash.data.SQLResult;
import flash.data.SQLStatement;
import flash.filesystem.File;
import mx.collections.ArrayCollection;
private var conn:SQLConnection;
private function init():void
{
conn = new SQLConnection();
conn.addEventListener(SQLEvent.OPEN, openSuccess);
//conn.addEventListener(SQLErrorEvent.ERROR, openFailure);
var dbFile:File = File.applicationDirectory.resolvePath(strGetDBName);
conn.openAsync(dbFile);
}
private function openSuccess(event:SQLEvent):void
{
conn.removeEventListener(SQLEvent.OPEN, openSuccess);
//conn.removeEventListener(SQLErrorEvent.ERROR, openFailure);
getData();
}
private function getData():void
{
var select:SQLStatement = new SQLStatement();
select.sqlConnection = conn;
//select.text = "SELECT id, txtState, txtAbbrev FROM " + strGetCurrentTableName;
select.text = "SELECT id, txtState FROM lkStates";
select.addEventListener(SQLEvent.RESULT, selectResult);
//select.addEventListener(SQLErrorEvent.ERROR, selectError);
select.execute();
}
private function selectResult(event:SQLEvent):void
{
var result:SQLResult = null;
result = event.currentTarget.getResult();
if(result.data)
{
list.dataProvider = new ArrayCollection(result.data);
}
}
This worked for me, I tried to match your variables for example.
var result:SQLResult = select.getResult();
list.dataProvider = new DataProvider(result.data);
if(result.data)
{
for(var i:int = 0; i < result.data.length; i++)
{
var tState:Object = result.data[i];
trace("var1 "+tState.var1+"var2 "+tState.var2...etc)
}
}
You need to set labelField to your list to a field name in your table so that it can appear in the list. In the declaration of your list type : labelField = "txtState" and you will get the information from this field.

newbie: flex netstream how to get my code stream and receive netstreams correctly?

I have problems getting my flex code to work, below is my code I try to netstream a webcam and receive it and use 2 functions for that. Any flex guru can help me fix these functions?
function onNetConnectionPublish():void {
StatusMessage("onNetConnectionPublish called");
ncNetStream = new NetStream(nc, NetStream.DIRECT_CONNECTIONS);
ncNetStream.addEventListener(NetStatusEvent.NET_STATUS, sendNetStreamHandler);
ncNetStream.publish("media");
ncNetStream.attachAudio(Microphone.getMicrophone());
ncNetStream.attachCamera(Camera.getCamera());
}
and:
function connectToRemote(remoteId:String) {
StatusMessage("connectToRemote(" + remoteId + ")");
ncNetStream = new NetStream(nc, remoteId);
ncNetStream.addEventListener(NetStatusEvent.NET_STATUS, receiveNetStreamHandler);
ncNetStream.play("media");
}
display video:
The Publisher Application:
private function Publisher():void{
var camera1:Camera = Camera.getCamera();
var video:Video = new Video(285, 254);
if (camera1)
{
video.attachCamera(camera1);
VideoDisplay1.addChild(video);
camera1.addEventListener(ActivityEvent.ACTIVITY, camera_activity);
camera1.addEventListener(StatusEvent.STATUS, camera_status);
}
var nc:NetConnection = new NetConnection();
nc.connect("rtmp://your/stream/url");
nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
function netStatusHandler(event:NetStatusEvent):void {
switch (event.info.code) {
case "NetConnection.Connect.Success":
var ns:NetStream = new NetStream(nc,NetStream.CONNECT_TO_FMS);
ns.attachCamera(camera1);
ns.publish("videofeed", "live");
break;
case "NetStream.Play.StreamNotFound":
trace("Unable to locate video: ");
break;
}
}
}
The Reciever Application :
import mx.utils.ObjectUtil;
private var nc:NetConnection;
private var ns:NetStream;
private var video:Video;
private var meta:Object;
private function init():void {
var nsClient:Object = {};
nsClient.onMetaData = ns_onMetaData;
nsClient.onCuePoint = ns_onCuePoint;
nc = new NetConnection();
nc.connect(null);
ns = new NetStream(nc);
ns.play("http://www.helpexamples.com/flash/video/cuepoints.flv");
ns.client = nsClient;
video = new Video();
video.attachNetStream(ns);
uic.addChild(video);
}
private function ns_onMetaData(item:Object):void {
trace("meta");
meta = item;
// Resize Video object to same size as meta data.
video.width = item.width;
video.height = item.height;
// Resize UIComponent to same size as Video object.
uic.width = video.width;
uic.height = video.height;
panel.title = "framerate: " + item.framerate;
panel.visible = true;
trace(ObjectUtil.toString(item));
}
private function ns_onCuePoint(item:Object):void {
trace("cue");
}
Reciever mxml code :
<mx:Panel id="panel" visible="false">
<mx:UIComponent id="uic" />
<mx:ControlBar>
<mx:Button label="Play/Pause" click="ns.togglePause();" />
<mx:Button label="Rewind" click="ns.seek(0); ns.pause();" />
</mx:ControlBar>
</mx:Panel>

Remote Image with basic authentication?

I would like to load a an image from an external domain and I have the below so far:
private function get_coverArt(coverArtID:String):void
{
var requestString:String = "/rest/getCoverArt.view?v=1.5.0&c=AirSub&id=" + coverArtID;
var requestURL:String = subServerURL + requestString;
myCoverArtLoader = new URLLoader();
var myRequest:URLRequest = new URLRequest();
var authHeader:URLRequestHeader = new URLRequestHeader();
authHeader.name = 'Authorization';
authHeader.value = 'Basic ' + credentials;
myRequest.requestHeaders.push(authHeader);
myRequest.url = requestURL;
myRequest.method = URLRequestMethod.GET;
myCoverArtLoader.dataFormat = URLLoaderDataFormat.BINARY;
myCoverArtLoader.addEventListener(Event.COMPLETE, set_coverArt);
myCoverArtLoader.load(myRequest);
}
private function set_coverArt(evt:Event) : void {
coverArtImg = new Image();
var loader:Loader = new Loader();
loader.loadBytes(myCoverArtLoader.data);
coverArtImg.source = loader;
}
This does not seem to work - any help?
Thanks!
Try setting the source directly like so:
private function set_coverArt(evt:Event) : void {
coverArtImg = new Image();
coverArtImg.source = myCoverArtLoader.data;
}
Also, check your authentication, here's a question I answered regarding the auth :
Actionscript 3: Reading an RSS feed that requires authentication

Flex 3: Setting one arrayCollection to another is stalling the application

I'll just go ahead and C/P the entire function to ensure you guys see everything going on:
public function directorsPrepsToShow():void
{
var tempDPrepsAC:ArrayCollection = new ArrayCollection;
var dprepSD:Date = new Date;
var dprepED:Date = new Date;
var viewSD:Date = rightDate(startViewDate.getMonth(), startViewDate.getDate(), startViewDate.getFullYear());
var viewED:Date = rightDate(viewSD.getMonth(), viewSD.getDate() + 14, viewSD.getFullYear());
var newACIDs:String = new String;
var useACIDs:String = new String;
for each (var item:Object in dPrepAC)
{
dprepSD = textToDate(item[2]);
dprepED = rightDate(dprepSD.getMonth(), Number(dprepSD.getDate() + (item[3] - 1)), dprepSD.getFullYear());
if (dateCollider(dprepSD, dprepED, viewSD, viewED))
tempDPrepsAC.addItem(item as Array);
}
if (tempDPrepsAC.length != usePrepAC.length)
{
usePrepAC = new ArrayCollection();
usePrepAC = tempDPrepsAC;
Alert.show("HI");
}
}
This function is in a separate file, that's called from the main mxml via the following:
<mx:Script source="functions/dprep.as" />
The line that's causing the app to stall is "usePrepAC = tempDPrepAC;". usePrepAC is declared in the main mxml like this:
[Bindable] public var usePrepAC:ArrayCollection = new ArrayCollection;
Dose anybody see why this one line would cause the application to stall? If I comment out that line, the application loads fine (loads everything except for the information that this AC should contain). I've been looking at this now for about an hour, trying different ways to get the contents of tempDPrepsAC into usePrepAC - but nothing is working. I tried googling it, but found nothing :(
Thanks,
Brds
EDIT
dprep AC is declared in the main mxml like this:
[Bindable] public var dPrepAC:ArrayCollection = new ArrayCollection;
And the function that populates it is as follows:
public function createDirectorsPrepCollection(e:ResultEvent):void
{
var xmlList:XMLList = XML(e.result).directorsprep;
var dupString:String = "|";
var tempArray:Array = new Array;
for (var i:int = 0; i < xmlList.length(); i++)
{
if (dupString.indexOf(String("|" + xmlList[i].name.#id) + "|") == -1)
{
tempArray = new Array;
tempArray[0] = xmlList[i].prepDBID;
tempArray[1] = xmlList[i].projectDBID;
tempArray[2] = xmlList[i].startdate;
tempArray[3] = xmlList[i].numdays;
tempArray[4] = xmlList[i].positions;
dPrepAC.addItem(tempArray);
dupString += "|" + xmlList[i].prepDBID + "|";
}
}
directorsPrepsToShow();
}
This function is called by this:
<mx:HTTPService id="dprepHttp" url="{dprepXML}" resultFormat="e4x" makeObjectsBindable="true" result="createDirectorsPrepCollection(event)" />
dPrepAC is populating fine btw... I check it in a for each loop.
Try using following code:
usePrepAC.source = tempDPrepsAC.source;

Resources