16 Replies Latest reply on Aug 6, 2008 1:18 PM by Peter Lorent

    Why won't this event handler bind?

    Mike_Thomsen
      I get an error when I try to call "doSomething()" at line 54 that says "call to possibly undefined method doSomething."

      Could someone explain what's going on here? Is this a scope issue or something? I don't understand why it is that a function defined at the top level <mx:Script> is invisible to that <mx:Image>.
        • 1. Re: Why won't this event handler bind?
          Peter Lorent Level 2
          Yeah, scope
          click="outerDocument.doSomething()
          • 2. Re: Why won't this event handler bind?
            Level 7

            "LuigiL" <webforumsuser@macromedia.com> wrote in message
            news:g79ut6$sln$1@forums.macromedia.com...
            > Yeah, scope
            > click="outerDocument.doSomething()

            Although better practice is to broadcast an event and let the application
            handle that.

            HTH;

            Amy


            • 3. Re: Why won't this event handler bind?
              M. Regert Level 1
              You can also move the itemrender code into it's own mxml component based on the VBox and then set the tileList item renderer to that component:

              • 4. Re: Why won't this event handler bind?
                Peter Lorent Level 2
                Than you would be moving code from the controller. Better dispatch a custom event then and keep the logic centralized.
                • 5. Re: Why won't this event handler bind?
                  Peter Lorent Level 2
                  Yeah Amy, I know, but I wasn't sure if the OP was interested in best practices at this point :-)
                  • 6. Re: Why won't this event handler bind?
                    Level 7

                    "LuigiL" <webforumsuser@macromedia.com> wrote in message
                    news:g7a11l$1qt$1@forums.macromedia.com...
                    > Yeah Amy, I know, but I wasn't sure if the OP was interested in best
                    > practices at this point :-)

                    I didn't doubt that you did, but the info wasn't for you LOL.


                    • 7. Re: Why won't this event handler bind?
                      Mike_Thomsen Level 1
                      What would be the best practice for firing off an event from the Image object on-click?
                      • 8. Re: Why won't this event handler bind?
                        Level 7

                        "Mike Thomsen" <webforumsuser@macromedia.com> wrote in message
                        news:g7a4t0$5u1$1@forums.macromedia.com...
                        > What would be the best practice for firing off an event from the Image
                        > object on-click?

                        It depends. Where/How are you using the image, and what kind of information
                        does your handler function need to get?

                        -Amy


                        • 9. Re: Why won't this event handler bind?
                          Mike_Thomsen Level 1
                          In an updated version, I have an Image next to the TileSet. What is the best way to have an event from an image inside the TileSet cause an attribute in the image next to the TileSet to change?
                          • 10. Re: Why won't this event handler bind?
                            Peter Lorent Level 2
                            From within your code it could be as simple as dispatching an event:
                            click="dispatchEvent(new Event(Event.OPEN, true))"
                            this event is set to bubble and the code would register a listener on the box that contains the itemrenderer
                            mybox.addEventListener(Event.OPEN, doSomething);
                            here the box that holds the itemrenderer has an id="mybox"

                            But, real best practice is to refactor this to controller (in this case the main application), a manager that gets the data (model, in effect the httpservice object) and a custom component for your view. And like mregert shows, a custom component for the itemrenderer, which btw I would prefer to write in Actionscript for better performance.
                            • 11. Re: Why won't this event handler bind?
                              Level 7

                              "Mike Thomsen" <webforumsuser@macromedia.com> wrote in message
                              news:g7a731$8cr$1@forums.macromedia.com...
                              > In an updated version, I have an Image next to the TileSet. What is the
                              > best way to have an event from an image inside the TileSet cause an
                              > attribute in the image next to the TileSet to change?

                              You may find that right-clicking on this
                              http://examples.adobe.com/flex2/inproduct/sdk/photoviewer/PhotoViewer.html
                              and selecting view source will give you a better idea of how to do this than
                              little snippets here and there from people on this forum.

                              HTH;

                              Amy


                              • 12. Re: Why won't this event handler bind?
                                cheftimo Level 2
                                This is an interesting thread and I would like to participate in the dialog a little, just for my own edification.

                                In this case, I think I would go for LuigiL's first reply. Here is why:

                                I don't usually like to see a component reaching into a parent because it creates a tight coupling, which is not so desirable in OOP – from what I hear, that is why it's considered a bad practice.

                                But I think this (the code in the initial post) is one of those few cases where this is OK. The itemRenderer is inline – you can't get much more tightly coupled than that already. True, it's still a separate component, but it's in the same file as its outerCocument, so it's not meant to be used by other applications or components, therefore it doesn't matter that it is tightly coupled.

                                I would probably feel differently if the renderer were a separate component. In that case, 'outerDocument' does not even work.

                                On his blog, Peter Ent wrote a three part essay on itemRenderers. It's very interesting and worth reading. Towards the end of Part 1, in the Summary, he simply says: " If you must refer to functions or properties in the containing file, use the outerDocument identifier to change the scope". I think Peter is one of the engineers that worked in designing itemRenderers at Adobe. If anyone wants to read the post, it's here: http://weblogs.macromedia.com/pent/archives/2008/03/itemrenderers_p.html.

                                There may be other times when using Application.application, parentDocuments and things like that is the proper thing to do and tat's why the engineers made them available.

                                What do you guys/girls think?

                                Carlos

                                Amy, what is HTH?
                                • 13. Re: Why won't this event handler bind?
                                  Level 7

                                  "cheftimo" <carlos@webgourmand.com> wrote in message
                                  news:g7b1le$691$1@forums.macromedia.com...
                                  > This is an interesting thread and I would like to participate in the
                                  > dialog a
                                  > little, just for my own edification.
                                  >
                                  > In this case, I think I would go for LuigiL's first reply. Here is why:
                                  >
                                  > I don't usually like to see a component reaching into a parent because it
                                  > creates a tight coupling, which is not so desirable in OOP ? from what I
                                  > hear,
                                  > that is why it's considered a bad practice.
                                  >
                                  > But I think this (the code in the initial post) is one of those few cases
                                  > where this is OK. The itemRenderer is inline ? you can't get much more
                                  > tightly
                                  > coupled than that already. True, it's still a separate component, but it's
                                  > in
                                  > the same file as its outerCocument, so it's not meant to be used by other
                                  > applications or components, therefore it doesn't matter that it is tightly
                                  > coupled.
                                  >
                                  > I would probably feel differently if the renderer were a separate
                                  > component.
                                  > In that case, 'outerDocument' does not even work.
                                  >
                                  > On his blog, Peter Ent wrote a three part essay on itemRenderers. It's
                                  > very
                                  > interesting and worth reading. Towards the end of Part 1, in the Summary,
                                  > he
                                  > simply says: " If you must refer to functions or properties in the
                                  > containing
                                  > file, use the outerDocument identifier to change the scope". I think Peter
                                  > is
                                  > one of the engineers that worked in designing itemRenderers at Adobe. If
                                  > anyone
                                  > wants to read the post, it's here:
                                  > http://weblogs.macromedia.com/pent/archives/2008/03/itemrenderers_p.html.
                                  >
                                  > There may be other times when using Application.application,
                                  > parentDocuments
                                  > and things like that is the proper thing to do and tat's why the engineers
                                  > made
                                  > them available.

                                  He goes on to say:

                                  "It is unreasonable to expect the itemRenderer to do the work. Afterall, the
                                  itemRenderer's job is to make the list look good - period. Event bubbling
                                  allows the itemRenderer to pass off the work to something else. A custom
                                  event is useful here because the event is related to the data on the row -
                                  so why not include that data in the event; the receiver of the event won't
                                  have to go hunt it down."

                                  > Amy, what is HTH?

                                  When I use it, it means Hope This Helps. Some people use it as Happy To
                                  Help.


                                  • 14. Re: Why won't this event handler bind?
                                    Peter Lorent Level 2
                                    I've refactored your code to implement a MVC like structure with a custom event to get the photos. If you're interested, let me know and I will post the code somewhere or let me know where to send the code.
                                    • 15. Re: Why won't this event handler bind?
                                      Mike_Thomsen Level 1
                                      mikerthomsen@gmail.com

                                      Thanks!
                                      • 16. Re: Why won't this event handler bind?
                                        Peter Lorent Level 2
                                        Got your mail address, you can remove it. I've send you an email where the source files are.