Connecting a callback action listener WITH parameters

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • K Viltersten

    Connecting a callback action listener WITH parameters

    When i'm using the following, everything works well.

    butt1.on ('click', function () {sameStuff ('1')});
    butt2.on ('click', function () {sameStuff ('2')});

    Now, i wish to compact it into one call, instead of two anonymous
    functions. The problem is that i'm required to send in a function as the
    second parameter. If i use the following i get only a return back, not
    typed as function.

    butt1.on ('click', function ('1'));

    How can i enter as the second parameter BUT with a specified parameter in
    it? Is it doable at all?

    --
    Regards
    Konrad Viltersten
  • david.karr

    #2
    Re: Connecting a callback action listener WITH parameters

    On May 22, 7:47 am, "K Viltersten" <t...@vilterste n.comwrote:
    When i'm using the following, everything works well.
    >
    butt1.on ('click', function () {sameStuff ('1')});
    butt2.on ('click', function () {sameStuff ('2')});
    >
    Now, i wish to compact it into one call, instead of two anonymous
    functions. The problem is that i'm required to send in a function as the
    second parameter. If i use the following i get only a return back, not
    typed as function.
    >
    butt1.on ('click', function ('1'));
    >
    How can i enter as the second parameter BUT with a specified parameter in
    it? Is it doable at all?
    I'm having trouble understanding what you're trying to do.

    Are you perhaps trying to define a single function that can act as the
    handler for both button clicks, and do something useful? If that's
    the case, just define the function before the two calls to "on",
    assigning it to a variable, and reference that variable in the two
    "on" calls.

    In the common event handler, you'll probably have to know which button
    was clicked, so you'll have to get the event object (either passed in
    as the single parameter, or the global event object) and get the
    target property.

    Does that help?

    Comment

    • =?ISO-8859-15?Q?=22=C1lvaro_G=2E_Vicario=22?=

      #3
      Re: Connecting a callback action listener WITH parameters

      K Viltersten escribió:
      When i'm using the following, everything works well.
      >
      butt1.on ('click', function () {sameStuff ('1')});
      butt2.on ('click', function () {sameStuff ('2')});
      >
      Now, i wish to compact it into one call, instead of two anonymous
      functions. The problem is that i'm required to send in a function as the
      second parameter. If i use the following i get only a return back, not
      typed as function.
      >
      butt1.on ('click', function ('1'));
      >
      How can i enter as the second parameter BUT with a specified parameter
      in it? Is it doable at all?
      According to Core JavaScript 1.5 Reference, you cannot:

      To pass parameters to an event handler, the handler must be
      wrapped into another function"

      document.form1. button1.onclick = function() {
      setBGColor('som e value');
      };

      Generally speaking, a function is a "subprogram" that can be called by code external (or internal, in the case of recursion) to the function. Like the program itself, a function is composed of a sequence of statements called the function body. Values can be passed to a function as parameters, and the function will return a value.



      --
      -- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
      -- Mi sitio sobre programación web: http://bits.demogracia.com
      -- Mi web de humor al baño María: http://www.demogracia.com
      --

      Comment

      • K Viltersten

        #4
        Re: Connecting a callback action listener WITH parameters

        >When i'm using the following, everything works well.
        > butt1.on ('click', function () {sameStuff ('1')});
        > butt2.on ('click', function () {sameStuff ('2')});
        > Now, i wish to compact it into one call, instead of two anonymous
        >functions. The problem is that i'm required to send in a function as
        >the second parameter. If i use the following i get only a return back,
        >not typed as function.
        > butt1.on ('click', function ('1'));
        > How can i enter as the second parameter BUT with a specified parameter
        >in it? Is it doable at all?
        >
        According to Core JavaScript 1.5 Reference, you cannot:
        >
        To pass parameters to an event handler, the handler must be
        wrapped into another function"
        >
        document.form1. button1.onclick = function() {
        setBGColor('som e value');
        };
        >
        http://developer.mozilla.org/en/docs...event_handlers
        Too bad! Thanks.

        --
        Regards
        Konrad Viltersten

        Comment

        • K Viltersten

          #5
          Re: Connecting a callback action listener WITH parameters

          >When i'm using the following, everything works well.
          >>
          > butt1.on ('click', function () {sameStuff ('1')});
          > butt2.on ('click', function () {sameStuff ('2')});
          >>
          >Now, i wish to compact it into one call, instead of two anonymous
          >functions. The problem is that i'm required to send in a function as the
          >second parameter. If i use the following i get only a return back, not
          >typed as function.
          >>
          > butt1.on ('click', function ('1'));
          >>
          >How can i enter as the second parameter BUT with a specified parameter
          >in
          >it? Is it doable at all?
          >
          I'm having trouble understanding what you're trying to do.
          >
          Are you perhaps trying to define a single function that can act as the
          handler for both button clicks, and do something useful? If that's
          the case, just define the function before the two calls to "on",
          assigning it to a variable, and reference that variable in the two
          "on" calls.
          >
          In the common event handler, you'll probably have to know which button
          was clicked, so you'll have to get the event object (either passed in
          as the single parameter, or the global event object) and get the
          target property.
          >
          Does that help?
          Nope. That, i had already. I wanted to do something even more compact. As
          mr Vicario pointed out, i will not get to do it the way i wanted. Thanks
          for the reply anyhow. It's appreciated. :)

          --
          Regards
          Konrad Viltersten

          Comment

          • david.karr

            #6
            Re: Connecting a callback action listener WITH parameters

            On May 22, 7:47 am, "K Viltersten" <t...@vilterste n.comwrote:
            When i'm using the following, everything works well.
            >
            butt1.on ('click', function () {sameStuff ('1')});
            butt2.on ('click', function () {sameStuff ('2')});
            >
            Now, i wish to compact it into one call, instead of two anonymous
            functions. The problem is that i'm required to send in a function as the
            second parameter. If i use the following i get only a return back, not
            typed as function.
            >
            butt1.on ('click', function ('1'));
            >
            How can i enter as the second parameter BUT with a specified parameter in
            it? Is it doable at all?
            Now that I understand what you're trying to do, note that YUI, and
            likely all of the major JS frameworks, make it easy to associate
            additional parameters with specific handler bindings. It's not worth
            doing this if that's all you need it for, but if you were making a
            list of reasons ...

            Comment

            • P. Prikryl

              #7
              Re: Connecting a callback action listener WITH parameters

              You can use something like this:

              function makeSameStuff(n ) {
              return function() { sameStuff(n); };
              }

              butt1.on("click ", makeSameStuff(" 1"));
              butt2.on("click ", makeSameStuff(" 2"));

              Comment

              • Thomas 'PointedEars' Lahn

                #8
                Re: Connecting a callback action listener WITH parameters

                Álvaro G. Vicario wrote:
                K Viltersten escribió:
                >When i'm using the following, everything works well.
                >>
                > butt1.on ('click', function () {sameStuff ('1')});
                > butt2.on ('click', function () {sameStuff ('2')});
                >>
                >Now, i wish to compact it into one call, instead of two anonymous
                >functions. The problem is that i'm required to send in a function as the
                >second parameter. If i use the following i get only a return back, not
                >typed as function.
                >>
                > butt1.on ('click', function ('1'));
                >>
                >How can i enter as the second parameter BUT with a specified parameter
                >in it? Is it doable at all?
                >
                According to Core JavaScript 1.5 Reference, you cannot:
                The Reference has zero relevance to this question.
                To pass parameters to an event handler, the handler must be
                wrapped into another function"
                >
                document.form1. button1.onclick = function() {
                setBGColor('som e value');
                };
                >
                http://developer.mozilla.org/en/docs...event_handlers
                This section should be removed as it advocates an obsolete practice
                and has nothing to do with the ("Core") programming language. Event
                handlers/listeners are part of the (Gecko) DOM API.


                PointedEars
                --
                realism: HTML 4.01 Strict
                evangelism: XHTML 1.0 Strict
                madness: XHTML 1.1 as application/xhtml+xml
                -- Bjoern Hoehrmann

                Comment

                • K Viltersten

                  #9
                  SV: Connecting a callback action listener WITH parameters

                  You can use something like this:
                  >
                  function makeSameStuff(n ) {
                  return function() { sameStuff(n); };
                  }
                  >
                  butt1.on("click ", makeSameStuff(" 1"));
                  butt2.on("click ", makeSameStuff(" 2"));

                  Do you mean that switching between quotation
                  marks and apostrophes makes that difference?

                  --
                  Regards
                  Konrad Viltersten
                  --------------------------------
                  sleep - a substitute for coffee for the poor
                  ambition - lack of sense to be lazy

                  Comment

                  • P. Prikryl

                    #10
                    Re: Connecting a callback action listener WITH parameters

                    On 23. Máj, 06:33 h., "K Viltersten" <t...@vilterste n.comwrote:
                    Do you mean that switching between quotation
                    marks and apostrophes makes that difference?
                    No, it does not matter whether you use single or double quotes. The
                    difference is the function makeSameStuff, which returns function that
                    is used as second argument of butt1.on and butt2.on.

                    Comment

                    • =?UTF-8?B?IsOBbHZhcm8gRy4gVmljYXJpbyI=?=

                      #11
                      Re: Connecting a callback action listener WITH parameters

                      Thomas 'PointedEars' Lahn escribió:
                      The Reference has zero relevance to this question.
                      >
                      > To pass parameters to an event handler, the handler must be
                      > wrapped into another function"
                      >>
                      > document.form1. button1.onclick = function() {
                      > setBGColor('som e value');
                      > };
                      >>
                      >http://developer.mozilla.org/en/docs...event_handlers
                      >
                      This section should be removed as it advocates an obsolete practice
                      and has nothing to do with the ("Core") programming language. Event
                      handlers/listeners are part of the (Gecko) DOM API.
                      So the syntax to assign a callback function with parameters is... :-?


                      --
                      -- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
                      -- Mi sitio sobre programación web: http://bits.demogracia.com
                      -- Mi web de humor al baño María: http://www.demogracia.com
                      --

                      Comment

                      • K Viltersten

                        #12
                        Re: Connecting a callback action listener WITH parameters

                        >Do you mean that switching between quotation
                        >marks and apostrophes makes that difference?
                        No, it does not matter whether you use single or double quotes. The
                        difference is the function makeSameStuff, which returns function that
                        is used as second argument of butt1.on and butt2.on.
                        Oh, NOW i see it. It took quite some time (and your
                        explaination) before it rang the bell. Thanks!

                        --
                        Regards
                        Konrad Viltersten

                        Comment

                        • Thomas 'PointedEars' Lahn

                          #13
                          Re: Connecting a callback action listener WITH parameters

                          Álvaro G. Vicario wrote:
                          Thomas 'PointedEars' Lahn escribió:
                          >The Reference has zero relevance to this question.
                          >> To pass parameters to an event handler, the handler must be
                          >> wrapped into another function"
                          >>>
                          >> document.form1. button1.onclick = function() {
                          >> setBGColor('som e value');
                          >> };
                          >>>
                          >>http://developer.mozilla.org/en/docs...event_handlers
                          >This section should be removed as it advocates an obsolete practice
                          >and has nothing to do with the ("Core") programming language. Event
                          >handlers/listeners are part of the (Gecko) DOM API.
                          >
                          So the syntax to assign a callback function with parameters is... :-?
                          I am talking about obsolete *practice*, _not_ syntax.

                          Maybe someone who does not mind your network abuse[1] takes the time
                          explaining the difference to you.


                          PointedEars
                          ___________
                          [1] http://tools.ietf.org/html/rfc1036
                          This document specifies a syntax for text messages that are sent between computer users, within the framework of "electronic mail" messages. [STANDARDS-TRACK]

                          --
                          realism: HTML 4.01 Strict
                          evangelism: XHTML 1.0 Strict
                          madness: XHTML 1.1 as application/xhtml+xml
                          -- Bjoern Hoehrmann

                          Comment

                          Working...