dcsimg
A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: Use a parameter as a dynamic method call

  1. #1
    Member
    Join Date
    Nov 2007
    Posts
    41

    Use a parameter as a dynamic method call

    Hi - wasn't sure how to word the title, best I could think of in regards to what I want to do.

    I'm using the manmachine updated RPC classes to make a web service call. Currently it only allows you to call a web service method by a given specific name - ie

    var token1:AsyncToken = service.myRemoteMethodName();

    I've two questions about this:

    a) firstly the function this call is in is written as such:

    Code:
         public function callService():void { }
    I want to be able to pass in a method name string as a param which is then used as a dynamic method to look at on the remote server. ie

    Code:
         public function callService(method:String):void {
    
    }
    First question is - how do I assign this param to the service. call?
    var token1:AsyncToken = service. (param goes here somehow)

    b) second question is I'm assuming if I can pass one param, I can pass another if the web service at the other end is set up to receive one? eg
    Code:
           public function callService(myMethod:String, data:XML) {
               var token1:AsyncToken = service.myMethod(data); 
    }
    any pointers would be great thanks

    frank

  2. #2
    rabid_Delineator AttackRabbit's Avatar
    Join Date
    Dec 2003
    Location
    Orlando, Florida
    Posts
    481
    You really could just set up a switch statement
    Code:
    switch ( method ) {
    	case 'serviceCallA' :
    	var token : AsyncToken = service.someService( data ); 
    	break;
    	case 'serviceCallB' :
    	var token : AsyncToken = service.someOtherService( data ); 
    	break;
    };
    the only real reason you would need to dynamically reference the name of the service was if it was constantly changing on the server side, or if you had hundreds of service calls you didn't want to hardcode.

  3. #3
    Member
    Join Date
    Nov 2007
    Posts
    41

    resolved

    Doh! Brain so tired, never thought of that simple way of doing it. Cheers.

  4. #4
    Member
    Join Date
    Nov 2007
    Posts
    41
    In addition to that, if I have a minimum of one param going in, ie method:String, but other methods could go in as well, how do you write it so that these extra ones are null if no data is sent to them?

    For example addEventListener needs ("anEvent" and aFunctionToFire); but it can also have true, 0, false after woulds for garbage collection and other mysteries. How do you program a function so that these extra params that are not filled dont cause the program to fall over?

    thanks

    frank

  5. #5
    Member
    Join Date
    Nov 2007
    Posts
    41
    In addition to that, if I have a minimum of one param going in, ie method:String, but other methods could go in as well, how do you write it so that these extra ones are null if no data is sent to them?

    For example addEventListener needs ("anEvent" and aFunctionToFire); but it can also have true, 0, false after woulds for garbage collection and other mysteries. How do you program a function so that these extra params that are not filled dont cause the program to fall over?

    thanks

    frank

  6. #6
    Senior Member
    Join Date
    May 2004
    Posts
    226
    I am doing something similar, basically I have a webService command queue and I store an array of queue objects that include stuff like methodName, params and event target.
    Try something like this:
    PHP Code:
            function callWebMethodmethodName:Stringparams:Array ):void {
                var 
    operation:Operation webService.getOperationmethodName );
                
    operation.arguments params;
                
    operation.send();
            } 

  7. #7
    rabid_Delineator AttackRabbit's Avatar
    Join Date
    Dec 2003
    Location
    Orlando, Florida
    Posts
    481
    well , i mean if you have no idea how many arguments you are going to have at any given time , you might consider assigning them all to an to an object. Then just write an extra conditional, but where ever it is that you are calling your callService method , set your object up :

    var tmpObj : Object = new Object();
    tmpObj.serviceType = 'serviceCallA';
    tmpObj.data = myDataXML;

    var addParams : Object = new Object();
    addParams.boolean1 = true;
    addParams.boolean2 = false;

    tmpObj.params = addParams;

    callService( tmpObj )

    public function callService( myMethodObj : Object ) {

    if( myMethodObj.params != null ) {
    //parse your params object
    };
    switch ( myMethodObj.serviceType ) {
    case 'serviceCallA' :
    var token : AsyncToken = service.someService( data );
    break;
    case 'serviceCallB' :
    var token : AsyncToken = service.someOtherService( data );
    break;
    };

    };

  8. #8
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    In answer to your first question - you can call functions (public functions or functions in the same scope...) using the bracket notation as a lookup:

    PHP Code:
    function a(s:String):void{
        
    this[s]("message from a");
    }

    function 
    b(s:String):void{
        
    trace('B's);    
    }

    function 
    c(s:String):void{
        
    trace('C's);    
    }

    a('b');     // B message from a
    a('c');     // C message from a 
    And for default values in a function signature you can use "=":

    PHP Code:
    function f(e:EventfuncToFire:Function, boolA:Boolean falsei:int 0boolB:Boolean false):void{
        

    And finally - if you don't know how many params you'll be receiving, you can use the rest (...) operator to collect 0-n parameters into an array as they're passed in:

    PHP Code:
    function g(...args):void{
        
    trace(args[0]); //  event
        
    trace(args[1]);    //  function
        
    trace(args[2]); //  undefined


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center