开发者

function name in xml and call this function

I have functions and xml file in which i store name of this functions and components..eg.

<commands>
  <command>
    <flexObject>myObject1</flexObject>
    <flexFunction>myFunction1</flexFunction>
  &l开发者_C百科t;/command>

  <command>
    <flexObject>myObject2</flexObject>
    <flexFunction>myFunction2</flexFunction>
  </command>
</commands>

i want make array of functions and then call them..like as

arr:Array = new Array(); 
arr.push(myObject1.myFunction1);
arr.push(myObject2.myFunction1);

arr[0]();

call myObject1.myFunction1 function

myObjects and myFunctions are classic component and their functions

when i call setCommandsService.send <s:HTTPService id="setCommandsService" url="commands.xml" result="setCommandsService_resultHandler(event)"/> in handler is name of this function as String and i dont know how can i add to array as function..


Okay, you need to make sure that those objects and functions are there, but should be possible by using the dynamic nature of actionscript:

var functions:Array = [];
for(var i:uint = 0, len:uint = xml.command.length(); i<len; i++)
{
   if(this[xml.command[i].flexObject] && this[xml.command[i].flexObject][xml.command[i].flexFunction])
   {
      functions.push(this[xml.command[i].flexObject][xml.command[i].flexFunction]);
   }
}

This would fill your array with direct references to the function, from here you just need to do functions[i]() to call them. With that said, I don't say I agree with having XML know about the internal working of your application. It could be possible for this xml to call anything from the outside, which is a definite security issue. If anything, try to abstract it to an 'action' ID that you parse in flex and then flex knows what to do.


Try:

this[arr[0]]();

or

var f:Function = Object[arr[0]] as Function; 
f.call();


You may need to use getDefinitionByName

import flash.utils.getDefinitionByName;

var function:Function = getDefinitionByName('namespace.myFunction1') as Function;

This only works on namespace-level functions though not static class methods

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜