Flash Vista - Home
Newest Cool Site
The Two Tales
Site info | Archive
Main Navigation
Home
New Links
Top Rated
Most Popular
Cool Sites
Search

Flash Tutorials
Flash Resources
Flash News
Flash Templates
Flash Intro Templates
Website Templates
Flash Games
Flash Books
FlashVista Polls
Sitemap


Random Link
Spanish
Enfoque grafico
Site info | Get another
FlashVista
Login
Register
Subscribe

Add Link
Modify Link
Favorites
Suggest Category

Advertise with us
Support us
Credits / Thanks
Contact

Flash Templates:

More Templates ...

Mailing List
User

Password



Register
Forgot password?
Partner websites
Flash Templates
Flash Components
Free Hit Counter
Free Seo Tools
Free Tutorials
Free Video Tutorials
Forum signatures
Other Resources
Suggest
Suggest this site to a friend


Mailing List
Status: Not logged in

English English German French Spanish Italian Portuguese Russian Polish Finnish Dutch Swedish Thai Romanian Traditional Chinese Simplified Chinese
SearchNot logged in
Keyword: Search for: Advanced Search


Knowing when the cfform data arrives


NEW Flash Tutorials in Video Format - Powered by LearnFlash.com: 45 minutes of flash tutorials now available in streaming format or download. Topics Include flash for beginners, text effects, actionscripting, audio/video, flash 8 and more.


Printer version



By As Fusion
http://www.asfusion.com/blog/


So you thought that onload would fix all of your problems. Say you wanted to select an item in the datagrid as soon as the form “loaded”. Great, you may have though, I’ll use onload. If you did, you may have discovered that the form loads, and then the data loads, and most likely, when you run your onload function the data has not yet arrived. So how do you know when it does?Some components (for some reason the tree doesn’t want to do it) trigger an event when their dataProvider changes. That event is called “modelChanged”. In order to be notified when that happens, add the following to your onload function:


var listener:Object = {};

listener.modelChanged = function(evt):Void {
alert('Data loaded');

}

myGrid.addEventListener('modelChanged',listener);


This will work for datagrids and selects. To remove a listener that you no longer need:


myGrid.removeEventListener('modelChanged',listener);

I’ll show three different uses of this on a datagrid: selecting the first item when the data loads, clear the grid to use the filter shown in Filtering a grid as you type – using functions and load an empty grid and populate it with remoting instead.

Selecting the first item





function onFormLoad(){
var listener:Object = {};

//put the controls in scope to avoid calling _root

var contactList:mx.controls.DataGrid = contactList;

listener.modelChanged = function(evt):Void {
alert('Data loaded... select first item');

contactList.removeEventListener('modelChanged',listener);

if (contactList.dataProvider.length){
contactList.selectedIndex = 0;
}
}

contactList.addEventListener('modelChanged',listener);
}









Run the filter function
This is a slightly changed version of the applyFilter function. I also changed its name to better describe what it does now





function onFormLoad(){
var listener:Object = {};

//put the controls in scope to avoid calling _root

var contactList:mx.controls.DataGrid = contactList;

listener.modelChanged = function(evt):Void {
alert('Data loaded... clear datagrid until department is selected');

contactList.removeEventListener('modelChanged',listener);

search('',contactList,[]);
}
listener.search = search;

contactList.addEventListener('modelChanged',listener);
}


function search( term:String, grid:mx.controls.DataGrid, columns:Array ):Void {

var filterTerm:String = term.toString().toLowerCase();

if(_global.unfilteredData[grid.id] == undefined){
if (_global.unfilteredData == undefined){
_global.unfilteredData = {};
}
_global.unfilteredData[grid.id] = grid.dataProvider.slice(0);
}

if(filterTerm.length > 0) {

var filteredData:Array = [];

for(var i = 0; i< _global.unfilteredData[grid.id].length; i++) {
var item:Object = _global.unfilteredData[grid.id][i];
var added:Boolean = false;

for(var j = 0; j< columns.length; j++){
if(!added){
var value:String = item[columns[j]].toString().toLowerCase();
if(value.indexOf(filterTerm) != -1) {
filteredData.push(item);
added = true;
}
}
else {
break;
}
}
}

grid.dataProvider = filteredData;

}
else {
grid.dataProvider = [];
}
}















Load it with remoting




function onFormLoad(){
var listener:Object = {};

//put the controls in scope to avoid calling _root

var getData:Function = getData;
var contactList:mx.controls.DataGrid = contactList;

listener.modelChanged = function(evt):Void {
alert('Empty data loaded... calling remoting');

contactList.removeEventListener('modelChanged',listener);
getData();
}

contactList.addEventListener('modelChanged',listener);
}

function getData():Void{

//create connection

var connection:mx.remoting.Connection = mx.remoting.NetServices.createGatewayConnection( "http://#cgi.HTTP_HOST#/flashservices/gateway/");
//declare service

var myService:mx.remoting.NetServiceProxy;


var responseHandler:Object = {};

//put the controls in scope to avoid calling _root

var contactList:mx.controls.DataGrid = contactList;

responseHandler.onResult = function( results: Object ):Void {
//when results are back, populate the cfgrid
contactList.dataProvider = results;
mx.managers.CursorManager.removeBusyCursor();
}

responseHandler.onStatus = function( stat: Object ):Void {
//if there is any error, show an alert
alert("Error while calling cfc:" + stat.description);
mx.managers.CursorManager.removeBusyCursor();
}

//get service, make sure you write the correct path
myService = connection.getService("flashRemotingResponder", responseHandler );
mx.managers.CursorManager.setBusyCursor();
//make call

myService.getMembers();
}










Live example 1
Live example 2
Live example 3

Download the source

Read the complete article here

(Added: 10-28-2005, Hits: 0, Rating: 0.00, Votes: 0, Reviews: 0)
Add to Favorites Suggest to a Friend

Reviews: (0)


Add Review
Please note:
We review EVERY comment before it appears on the site, so please dont waste your time by posting spam links :)
No URLs allowed, no HTML please.

If you register or login first, your review will contain your nickname


Rate It



Excellent!
Very Good
Good
Fair
Poor