Populating related cfselects via Flash Remoting

First of let me say that as much as I would love this to be my code it mainly comes out of two posts [1,2] on ASFusion.com.

To sidetrack for a moment, I must point out now is that even if you don't find answers in the Asfusion posts always check the comments out! They get up to 50+ comments on some posts with hints and tips there as well.

Back to the form. I had a project which started life as a 5 page HTML form with the need to make it a little user friendly to the eyes and more organised. Any form over 2 pages I hate in general and I seem to be the only person in the world who thinks one long page is better than X amount of multiple pages this is whether i'm filling them in or creating them.

In moving my HTML form over to <cfform format="flash"> I ran into the problem of having to duplicate some Ajax where one select is populated from a DB then another select is populated from there. It didn't take too much digging around and I thought i'd share in case it helps anyone else out. My Flash/As knowledge isn't 100% so if you see an easier way drop me a line in the comments.

First of the CFC

flashRemotingResponder.cfc

queryAddRow(brandQry); querySetCell(brandQry,'brand_code','dell'); querySetCell(brandQry,'brand_name','Dell'); queryAddRow(brandQry); querySetCell(brandQry,'brand_code','apple'); querySetCell(brandQry,'brand_name','Apple Inc.'); queryAddRow(brandQry); querySetCell(brandQry,'brand_code','microsoft'); querySetCell(brandQry,'brand_name','Microsoft Corp.'); SELECT brand_code, brand_name FROM brandQry queryAddRow(modelQry); querySetCell(modelQry,'model_number','123-qwe'); querySetCell(modelQry,'model_description','Apple Mac Book Pro 15.4"'); querySetCell(modelQry,'brand_code','apple'); queryAddRow(modelQry); querySetCell(modelQry,'model_number','987123-123'); querySetCell(modelQry,'model_description','Apple Mac Book Pro 17"'); querySetCell(modelQry,'brand_code','apple'); queryAddRow(modelQry); querySetCell(modelQry,'model_number','asdpo-1232'); querySetCell(modelQry,'model_description','Apple 72" Monitor'); querySetCell(modelQry,'brand_code','apple'); queryAddRow(modelQry); querySetCell(modelQry,'model_number','d123'); querySetCell(modelQry,'model_description','XPS Mobile Gaming Laptop'); querySetCell(modelQry,'brand_code','dell'); queryAddRow(modelQry); querySetCell(modelQry,'model_number','d6534'); querySetCell(modelQry,'model_description','64" Monitor'); querySetCell(modelQry,'brand_code','dell'); queryAddRow(modelQry); querySetCell(modelQry,'model_number','123-mqwe'); querySetCell(modelQry,'model_description','DRM removal tool'); querySetCell(modelQry,'brand_code','microsoft'); queryAddRow(modelQry); querySetCell(modelQry,'model_number','123-serr'); querySetCell(modelQry,'model_description','Windows Vista inc. SP4'); querySetCell(modelQry,'brand_code','microsoft'); SELECT model_number, model_description FROM modelQry WHERE brand_code =

Second the action <cfform>

flashRemoting.cfm

//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 = {}; //put the controls in scope to avoid calling _root var brand = brand; responseHandler.onResult = function( results: Object ):Void { //when results are back, populate the cfselect brand.labelField = "brand_name"; brand.dataProvider = results; } responseHandler.onStatus = function( stat: Object ):Void { //if there is any error, show an alert alert("Error while calling cfc:" + stat.description); } //get service myService = connection.getService("flashRemotingResponder", responseHandler ); //make call myService.getBrands(); //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 = {}; //put the controls in scope to avoid calling _root var model = model; responseHandler.onResult = function( results: Object ):Void { //when results are back, populate the cfselect model.labelField = "model_description"; model.dataProvider = results; } responseHandler.onStatus = function( stat: Object ):Void { //if there is any error, show an alert alert(stat.description); } //get service myService = connection.getService("flashRemotingResponder", responseHandler ); //make call myService.getModels(brand.selectedItem.brand_code);

You can download the source at: http://www.andyjarrett.co.uk/flash_remoting_populate_select.zip


comments powered by Disqus