Flex Syntax Error - apache-flex

I am getting Syntax Error 1202 (Access of undefined property connection in package model) in the following code while trying to access the model.connection property. I don't see any reason why this would appear, can anyone see something I may be overlooking?
Model.as
package valueObjects
{
import flash.data.SQLConnection;
import mx.collections.ArrayCollection;
public class Model
{
public var connection:SQLConnection;
public var albums:ArrayCollection = new ArrayCollection();
public var albumItems:ArrayCollection = new ArrayCollection();
public var selectedAlbum:Number = 0;
public var selectedItem:Number = 0;
public function Model()
{
}
}
}
And the actual code in my default mxml file, init() is called on initialize
import model.ModelLocator;
import mx.core.mx_internal;
import valueObjects.Model;
protected var sqlConnection:SQLConnection;
private var model:Model = new Model();
protected function init():void
{
createDb();
navigator.firstViewData = model;
}
protected function createDb():void
{
sqlConnection = new SQLConnection();
sqlConnection.open(File.applicationStorageDirectory.resolvePath("Oxford.db"));
var stmt:SQLStatement = new SQLStatement();
stmt.sqlConnection = sqlConnection;
stmt.text =
"CREATE TABLE IF NOT EXISTS albumItems (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"album INTEGER, " +
"name STRING, " +
"dateAdded DATE)";
stmt.execute();
model.connection = sqlConnection;
}

The issue here is that you have a package and a variable named 'model'. When you try to access the variable named model, it thinks you are referring to the package. If you correct this naming collision, you will see that this issue is fixed.

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.

cakephp user authentication for adobe air app

I have a web application developed using flex and cakephp. My client need to make a desktop application of that web application using Adobe Air. The conversion of the flex to Air is done successfully. I the flex application the communication of flex and cakephp is handled using a remotes controller.
In air application I have a problem of authenticating the user with cakephp default user authentication. Can anyone help me to find a solution for this?
i suggest you to send your user credentials via POST to your cakephp backend.
A login function in your UsersController would look something like this:
public function login() {
if ($this->Auth->login()) {
$this->serviceResponse(Status::SUCCESS);
} else {
$this->serviceResponse(Status::AUTH_FAILED);
}
}
// this is just an example out of my appcontroller to send json responses
public function serviceResponse($code, $data = array()) {
$response = compact('code', 'data');
$this->response->body(json_encode($response));
$this->response->send();
$this->shutdownProcess();
exit;
}
// I also defined a class for return statuses
class Status {
const SUCCESS = 'success';
const ERROR = 'error';
...
}
Basically, you want to send the validation request as an ajax request. To do that you need to modify headers, capture session ids through cookies and post them to keep the session alive. It expects a JSON object returned from Cake.
I've created a couple Flex classes that achieve just this for a Flex Mobile App and a CakePHP backend. It should work the same for your needs.
It's in two files, the AutoValidationUrlRequest.as extends the HeaderURLRequest.as file. I'm not positive but there may be a couple variables to change, but overall it works very well and probably won't take more than a couple changes to get it working on your app.
To use it, simply create a new AutoValidationUrlRequest object and add an event listener on the headerUrlRequestComplete Event and then run AutoValidationUrlRequest.send(...) to POST. there is also a handy method called convertToPostVars for easy Cake Friendly Post variables.
AutoValidationUrlRequest.as:
package libs
{
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.HTTPStatusEvent;
import models.LocalDeviceData;
import models.Model;
import mx.collections.ArrayCollection;
import mx.core.FlexGlobals;
import mx.utils.ObjectUtil;
[Event("LoginFailed")]
[Event("MobileUserDoesNotExist")]
public class AutoValidationURLRequest extends HeaderURLRequest
{
public static const LOGIN_FAILED:String = "LoginFailed";
public static const MOBILE_USER_DOES_NOT_EXIST:String = "MobileUserDoesNotExist";
/**
* will automatically be set by this class, if not set will login
*/
public var requestHeaders:Object = new Object();
private var _cookie:Object;
/**
* should be an object with name of the cookie variables parsed in as key/value pairs
*/
protected function set cookie(ck:Object):void{
_cookie = ck;
}
protected function get cookie():Object{
return _cookie;
}
public function AutoValidationURLRequest(){
};
public function send(url:String, postData:Object, generateUrlVars:Boolean = true):void{
if(generateUrlVars){
postData = convertToPostVars(postData);
}
sendRequest("http://yourwebsite.com"+url, postData, requestHeaders);
}
override protected function parseHeaders(e:HTTPStatusEvent):void{
super.parseHeaders(e);
if('Set-Cookie' in headers){
requestHeaders['Cookie'] = parseCookie(headers['Set-Cookie']);
//requestHeaders['User-Agent'] = headers['User-Agent'];
}
}
/**
* returns the cookie key/val string for send requests back to the server
*/
protected function parseCookie(cookieString:String):String{
var cookieObj:Object = new Object();
var cookieBits:Array = cookieString.split("; ");
return cookieBits[0];
/*
for each(var ck:String in cookieBits){
var cb:Array = ck.split("=");
cookieObj[cb[0]] = cb[1];
}
return cookieObj;
*/
}
}
}
HeaderURLRequest.as:
package libs
{
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.HTTPStatusEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestHeader;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
import mx.core.FlexGlobals;
import mx.rpc.events.ResultEvent;
import mx.utils.ObjectUtil;
[Event("headerUrlRequestComplete")]
public class HeaderURLRequest extends EventDispatcher
{
public static const HEADERURLREQUEST_COMPLETE:String = "headerUrlRequestComplete";
public var headers:Array = [];
public var data:Object = new Object();
public var variables:Object = new Object();
public var invalidFields:Object = new Object();
public var errorMsg:String = "";
/**
* the headers array must contain an object with a 'name' key and a 'value' key eg: cookie: <cookieStr>
*/
public function HeaderURLRequest():void{
}
public function sendRequest(url:String, postData:Object = null, requestHeaders:Object = null):void{
var urlLoader:URLLoader = new URLLoader()
var urlRequest : URLRequest = new URLRequest(url);
//make it an ajax request
urlRequest.requestHeaders.push(new URLRequestHeader('X-Requested-With', 'XMLHttpRequest'));
for(var header:* in requestHeaders){
var authHeader:URLRequestHeader = new URLRequestHeader(header as String, requestHeaders[header]);
urlRequest.requestHeaders.push(authHeader)
}
var urlVariables:URLVariables = new URLVariables();
for (var vars:* in postData){
urlVariables[vars] = postData[vars];
}
urlRequest.method = URLRequestMethod.POST
urlRequest.data = urlVariables;
urlLoader.addEventListener(Event.COMPLETE, getData);
urlLoader.addEventListener(HTTPStatusEvent.HTTP_RESPONSE_STATUS, parseHeaders);
urlLoader.load(urlRequest);
}
public function convertToPostVars(postData:Object, prependKeyName:String = ""):Object{
var params:Object = {};
if(prependKeyName == ""){
params['_method'] = 'POST';
}
for (var item:* in postData){
var objtype:Object = ObjectUtil.getClassInfo(postData[item]);
if(objtype.name == "Object"){
var modelKeyName:String = prependKeyName+"["+item+"]";
var subParams:Object = convertToPostVars(postData[item],modelKeyName);
params = merge(params, subParams);
}else{
params["data"+prependKeyName+"["+item+"]"] = postData[item];
}
}
return params;
}
public function flashErrorMsg():String{
var err:String = errorMsg;
errorMsg = "";
return err;
}
protected function parseHeaders(e:HTTPStatusEvent):void{
var i:Number=0;
headers = [];
for each(var header:URLRequestHeader in e.responseHeaders){
headers[header.name] = header.value;
i++;
}
}
protected function getData(e:Event):void{
//trace('data: ');
if(e.currentTarget.data == ''){
e.currentTarget.data = '{}';
}
data = JSON.parse(e.currentTarget.data);
//trace(ObjectUtil.toString(data));
if(data.hasOwnProperty('variables')){
variables = data.variables;
if (variables != null){
if(variables.hasOwnProperty('invalidFields')){
invalidFields = variables.invalidFields;
for (var error:String in invalidFields){
errorMsg += invalidFields[error] + "\n\n";
}
}
}else{
//no variable data found!!
}
}
dispatchEvent(new Event(HEADERURLREQUEST_COMPLETE));
}
public function isEmpty(obj:Object){
var isEmpty:Boolean = true;
for (var n in obj) { isEmpty = false; break; }
return isEmpty;
}
public function merge( obj0:Object, obj1:Object ):Object
{
var obj:Object = { };
for( var p:String in obj0 )
{
obj[ p ] = ( obj1[ p ] != null ) ? obj1[ p ] : obj0[ p ];
//trace( p, ' : obj0', obj0[ p ], 'obj1', obj1[ p ], '-> new value = ', obj[ p ] );
}
for (var p1:String in obj1){
if(!obj.hasOwnProperty(p1)){
obj[ p1 ] = obj1[ p1 ] ;
}
}
return obj;
}
}
}
Also, using this with my forked version of Jose Gonzalez's CakePHP ajax_controller plugin is really handy. It basically takes any Ajax Request and converts all view variables and outputs them into a JSON object, no rendered view. Otherwise, if you're not using an ajax request, Cake will render the views normally. Good Luck!

Dynamic tree in 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

Warnings in FileReference - can not find the cause

I'm trying to make a loader of a few photos (use FileReference). I get the warnings, but I do not know the reason for their appearance.
warning: unable to bind to property 'fr' on class 'Object' (class is not an IEventDispatcher)
warning: unable to bind to property 'name' on class 'flash.net::FileReference'
warning: unable to bind to property 'data' on class 'flash.net::FileReference'
warning: unable to bind to property 'fr' on class 'Object' (class is not an IEventDispatcher)
warning: unable to bind to property 'name' on class 'flash.net::FileReference'
warning: unable to bind to property 'data' on class 'flash.net::FileReference'
import mx.events.CollectionEvent;
import flash.net.FileReferenceList;
import mx.collections.ArrayCollection;
[Bindable]
private var photos:ArrayCollection = new ArrayCollection;
private var frList:FileReferenceList = new FileReferenceList;
private function init():void
{
photos.addEventListener(CollectionEvent.COLLECTION_CHANGE,function():void
{
startUploadButton.enabled = (photos.length>0);
clearPhotosButton.enabled = (photos.length>0);
});
frList.addEventListener(Event.SELECT,addPhotos);
}
private function selectPhotos():void
{
var fileFilter:FileFilter = new FileFilter("Images jpeg","*.jpg;*.jpeg");
frList.browse([fileFilter]);
}
private function addPhotos(e:Event):void
{
for (var i:uint = 0; i < frList.fileList.length; i++)
{
var elem:Object = new Object;
elem.fr = FileReference(frList.fileList[i]);
elem.fr.load();
elem.fr.addEventListener(Event.COMPLETE,refreshThumb);
photos.addItem(elem);
}
}
private function refreshThumb(e:Event):void
{
photosList.invalidateList();
}
public function clearPhoto(data:Object):void
{
photos.removeItemAt(photos.getItemIndex(data));
photosList.invalidateList();
}
private function startUpload():void
{
photosProgressContainer.visible = true;
var request:URLRequest = new URLRequest();
request.url = "http://localhost/tempLoader-debug/upload.php";
var fr:FileReference = photos.getItemAt(0).fr;
fr.cancel();
fr.addEventListener(ProgressEvent.PROGRESS,uploadProgress);
fr.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,uploadComplete);
fr.upload(request);
}
private function uploadProgress(e:ProgressEvent):void
{
photosProgress.setProgress(e.bytesLoaded,e.bytesTotal);
}
private function uploadComplete(e:DataEvent):void
{
photos.removeItemAt(0);
photosList.invalidateList();
if (photos.length > 0)
startUpload();
else
photosProgressContainer.visible = false;
}
This warnings occur because you try to bind to properties fr, FileReference.name and FileReference.data in your item renderer or somewhere. It may not bother you (don't know all you code) but to avoid them do the following:
Strong-typed data provider
Fill photos with objects of special class like:
public class Photo
{
public function Photo(fileReference:FileReference)
{
this.fileReference = fileReference;
}
public var fileReference:FileReference;
[Bindable("__NoChangeEvent__")] // __NoChangeEvent__ is a special name
public function get name():String
{
return fileReference.name;
}
[Bindable("__NoChangeEvent__")]
public function get data():*
{
return fileReference.data;
}
}
Then replace the code:
var elem:Object = new Object;
elem.fr = FileReference(frList.fileList[i]);
elem.fr.load();
elem.fr.addEventListener(Event.COMPLETE,refreshThumb);
photos.addItem(elem);
With the following:
var elem:Photo = new Photo(frList.fileList[i]);
elem.fileReference.addEventListener(Event.COMPLETE,refreshThumb);
elem.fileReference.load();
photos.addItem(elem);
You should also change all the code that uses photos collection accordingly.

Resources