Calling an ActionScript3 method from a Item Render in Flex

After receiving an email from Adobe detailing an application to load XML data into a DataGrid (written by Christoph Rooms) I hit a wall when trying to call a method in my actionscript. The DataGrid column had an mx:Image contained within mx:Component in an itemRenderer so that a little icon image can be displayed according to the xml data.

code

When I edited click=”…” and inserted my reference to an actionscript method I encountered:

1180: Call to a possibly undefined method myMethod

So I scoured the net, help files and saw ways of tackling this issue, some involving (if I understood them correctly) creating a custom event class then calling a function which is situated in an mx:Script tag which itself is within the mx:Image tag ?:)?

Anyway, the solution was in time found (thanks to my flex bible) and it is irritatingly simple. It turns out as always its a scope issue, the mx:Component tag defines a new scope where the local scope of the item renderer is defined by the MXML code.

click="outerDocument.myFunction();"

So the outerDocument reference to call the function on the containing page saves the day!!! cool…

This entry was posted in AS3, Flex, MXML. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

27 Comments

  1. Will
    Posted July 25, 2007 at 6:40 am | Permalink

    Well done.. just encountered this problem. Like you say simple solution, but one not easy to find.

    Cheers…

    Will

  2. Artacus
    Posted August 18, 2007 at 12:43 am | Permalink

    Haha, I got the same email, tried the same thing and hit the same wall.

    Would have been great if Adobe had actually included an example of this in their sample app.

  3. Ben
    Posted September 2, 2007 at 10:36 pm | Permalink

    Just had exactly this issue with my own code – thanks for the solution.

  4. Peter
    Posted September 27, 2007 at 5:33 pm | Permalink

    great.. Thank You.. Just have this issue… thanks for the solution

  5. Nigel
    Posted October 26, 2007 at 4:20 pm | Permalink

    Just to add that ‘myFunction’ obviously now cannot be private.

  6. Jason
    Posted November 11, 2007 at 5:08 am | Permalink

    Many thanks for posting this! I was pulling my hair out until I came across this

  7. Posted November 16, 2007 at 12:10 am | Permalink

    var websites: Array = [

    {label:"Yahoo", data:"http://www.yahoo.com"},
    {label:"Google", data:"http://www.google.com"},
    {label:"Hello-World", data:"http://www.hello-world.com"}
    ];

    cboLinks.dataProvider = new DataProvider(websites);
    cboLinks.addEventListener(Event.CHANGE, linktoPage);

    function linktoPage(event:Event): void {

    var site: URLRequest = new URLRequest();
    site.url = cboLinks.selectedItem.data;
    navigateToURL(site);

    }

    I also get error 1180 for that which is lol calling to undefined methoddddd. Is it a similar problem? Guessing now…but anyone know?

  8. Posted November 16, 2007 at 12:11 am | Permalink

    the Data Provider part is what is getting me that error

  9. Tom
    Posted January 30, 2008 at 11:56 pm | Permalink

    Any thoughts on how to pass data to the outerDocument function? click=”outerDocument.myFunction(data.foo);” doesn’t seem to work and click=”outerDocument.myFunction({data.foo});” doesn’t compile.

  10. Posted October 22, 2008 at 3:36 pm | Permalink

    Just had the exact same problem..
    thx for the info :-)

  11. Posted December 9, 2008 at 2:43 pm | Permalink

    Any thoughts on how to pass data to the outerDocument function? click=”outerDocument.myFunction(data.foo);” doesn’t seem to work and click=”outerDocument.myFunction({data.foo});” doesn’t compile.

    I would love to know the answer to this too

  12. Posted December 9, 2008 at 3:38 pm | Permalink

    Hi Nikos,

    Where are you pulling in data.foo from?

    Look here at this example I have just built for you, it shows a String being passed to the function in the outer document and then output into a Label text field below, view source is enabled.

    HTH,

    Simon

    http://www.nutrixinteractive.com/apps/grid_example/GridTest.html

  13. Posted February 11, 2009 at 11:37 pm | Permalink

    Hello All,

    Got a similar problem but my ItemRenderer is another component. Therefore the outerDocument.myFunc(); did not work. But I was lucky to guess the solution. If your ItemRenderer is another component, then the below works just fine.

    click=”parentDocument.myFunc();”

    Cheers,

    D.

  14. Rohit Marathe
    Posted February 19, 2009 at 11:11 am | Permalink

    Thanks Doruk Eker for the gr8 may to use parentDocument.myFunction() method,
    For other please make sure that myFunction() is a public function, otherwise you will get a runtime error.

  15. Rohit Marathe
    Posted February 19, 2009 at 11:12 am | Permalink

    Excellent

  16. joey
    Posted March 2, 2009 at 10:53 pm | Permalink

    Ahh, thanks! Who wouldve thunk?

  17. oleqsa
    Posted March 20, 2009 at 6:51 pm | Permalink

    Blessing on your head!!! ))))
    it’s a shame that they don’t talk about this kind of problems when making video tutorials (((

  18. Posted April 12, 2009 at 11:52 am | Permalink

    Hi guys,

    thanks sooo much, even to Nigel!!!

  19. Andy S
    Posted May 13, 2009 at 11:36 am | Permalink

    Thanks – that solved my issue.

  20. Jericho
    Posted September 11, 2009 at 4:52 am | Permalink

    THANK YOU THANK YOU. This saved my bacon.

  21. Posted October 16, 2009 at 4:00 pm | Permalink

    Thanks a lot for this article, been searching for a solution for ages, and thanks to Doruk for your solution with external components, I can now continue my project properly :D

  22. Armando flores
    Posted October 22, 2009 at 7:07 pm | Permalink

    Excellent solution my friends.

    thanks a lot ¡¡¡

  23. Andrew
    Posted October 28, 2009 at 6:41 pm | Permalink

    You Rock!

    I was searching all over for why this was happening.

    Thanks!

  24. Bill Gray
    Posted November 2, 2009 at 5:01 pm | Permalink

    Me too! Thanks

  25. Posted November 18, 2009 at 12:49 pm | Permalink

    @Tom @Nikos: Make sure the methods you’re calling are marked as public. private and protected won’t cut it.

  26. De Sperate
    Posted January 25, 2010 at 3:14 pm | Permalink

    Thanks man, just what I needed.
    These are just the kind of stupid problems that make one dream about quitting the whole scene after a few hours of debugging :)

  27. Norman
    Posted April 27, 2010 at 5:11 am | Permalink

    thanks! :)

One Trackback

  1. [...] via: nutrixinteractive [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>