当函数被定义为父mxml文档中的公共函数时,如何从包含的.as文件中调用函数?

时间:2013-07-15 00:06:06

标签: actionscript-3 flash flex flash-builder

我有一个案例,我在flashbuilder 4.6(apache flex sdk)中使用了一个包含的.as文件(会发布代码,但由于代码量的大小,我认为它会有点过分)...包括文件有一些运行正常的AS3代码,但我刚刚添加了对PARENT mxml文档中定义的函数的调用...我得到一个错误,报告包含的.as文件不知道函数是什么 - 好像它根本不存在。

我是否需要附加函数调用的开头?我在几天前从另一个项目中知道我在一些工作中发现了我的第一个严重的itemrenderer尝试,我不得不使用outerDocument。在功能的前面......即 -

outerDocument.MyFunctionNameHere();

只要我的父文档将函数定义为公共函数,它就应该对包含的.as文件中的代码“可见”,对吗?

1 个答案:

答案 0 :(得分:0)

希望这会有所帮助。

includes.as - 包含文件

// ActionScript file
public function includedFunction(item:Object):String{
    var data = parentFunction(item); //call function from parent mxml
    return data;
}

AS3方式(但不可绑定)

<fx:Script>
    <![CDATA[        
    include "includes.as";

    public function parentFunction(data:Object):String{             
        return "Hello From Main MXML";
    }
    ]]>
</fx:Script>
<s:DataGrid dataProvider="{new ArrayCollection(new Array(1,2,3))}">
    <s:columns>
        <s:ArrayList>
           <s:GridColumn>
             <s:itemRenderer>
                <fx:Component>
                  <s:GridItemRenderer>
                    <s:Label text="{outerDocument['includedFunction'].call(null, new Array(data))}"/>
                  </s:GridItemRenderer>
                </fx:Component>
             </s:itemRenderer>
           </s:GridColumn>
        </s:ArrayList>          
     </s:columns>
</s:DataGrid>

正确的方式

<fx:Script source="includes.as" />
<fx:Script>
    <![CDATA[   
    public function parentFunction(data:Object):String{             
        return "Hello From Main MXML";
    }
    ]]>
</fx:Script>
<s:DataGrid dataProvider="{new ArrayCollection(new Array(1,2,3))}">
    <s:columns>
        <s:ArrayList>
           <s:GridColumn>
             <s:itemRenderer>
                <fx:Component>
                  <s:GridItemRenderer>
                    <s:Label text="{outerDocument.includedFunction(data)}"/>
                  </s:GridItemRenderer>
                </fx:Component>
             </s:itemRenderer>
           </s:GridColumn>
        </s:ArrayList>          
     </s:columns>
</s:DataGrid>