if we can access outer function from rendered item by using “outerdocument” object then cant we access inner function from outside.?

StackOverflow https://stackoverflow.com/questions/12140137

Вопрос

These are the scripts under same application first one is under application and second is under dataGrid. from first script through outerClick() i am accessing a inner() function in itemrenderer. Well I can easily access the function outer() from innerClick() by outerDocument object but what_to_use to access inner() function in itemrenderer from outerClick() function. i tried with mx:component id"" and also class="" but it is not able to identify functions under itemrenderer.

  <mx:Script>
    <![CDATA[

        public function outer():void{
            Alert.show("Hi i am outer object");
            }
        public function outerClick():void{
            what_to_use.inner();
        }
    ]]>
</mx:Script>

This below item is rendered under data grid.

<s:itemRenderer>
        <mx:Component>
            <s:GridItemRenderer>
                <fx:Script>
                    <![CDATA[
                            public function innerClick():void{
                                outerDocument.outer();
                            }
                            public function inner():void{
                                Alert.show("Hi i am inner");
                            }
                    ]]>
                </fx:Script>
                    <s:CheckBox id="sel" selected="{data.checked=sel.selected}" change="{data.checked}" click="innerClick()"/>                                  
            </s:GridItemRenderer>
        </mx:Component>
    </s:itemRenderer>
Это было полезно?

Решение

if we can access outer function from rendered item by using “outerdocument” object then cant we access inner function from outside.?

You seem to have a distinct misunderstanding of what itemRenderers are. An itemRenderer is a component definition, sort of like creating your own class. since you are using your the 'outerDocument' reference, you are creating your own class in-line / embedded within another class.

An instance of your custom itemRenderer class is created for every visible item in your List based class; so your own class is created multiple times, which means there is more than 1 instance of your itemRenderer function. How would Flex know which itemRenderer instances you want to call the function on? In fact it can't.

That is why you can call up, but you cannot call down.

If you need to make changes inside the itemRenderer, you should do so by changing the data elements of the dataProvider.

For all intents and purposes, I recommend against using outerDocument too. It is a break in encapsulation. A component should never try to access its' parent; because that often introduces an unneeded dependency to your component, limiting reuse. The proper way to "communicate up" is to dispatch an event from the itemRenderer and listen for it in the parent class.

This concept is covered briefly on the answer to one of your previous questions.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top