Loading ...
Sorry, an error occurred while loading the content.
 

Re: Problem with callback method

Expand Messages
  • Sagar Dahiya
    ... The code is something like this: MyClass = function(url) { this.url = url; } MyClass.prototype = { changed: function() { with(this) { var obj =
    Message 1 of 7 , Jan 3, 2007
      --- In ydn-javascript@yahoogroups.com, "Thomas S. Sha" <tsha@...> wrote:
      >
      > --- In ydn-javascript@yahoogroups.com, "Sagar Dahiya"
      > <sagardahiya@> wrote:
      > >
      > > Hi,
      > >
      > > I am using a class, which defines a callback, as a prototype for that
      > > class. For success and failure in the callback, i'm calling some other
      > > prototype functions of that class(eg success: this.handleSuddess).
      > > This isn't working out for me and it doesnt even give any error
      > > message(The function is not even getting called). Can somebody help me
      > > out with this problem?
      > >
      > > Thanks,
      > > Sagar
      >
      > Can you post your functional script so I can see what's happening?
      >
      > Regards,
      > Thomas
      >

      The code is something like this:

      MyClass = function(url) {
      this.url = url;
      }

      MyClass.prototype = {

      changed: function() {
      with(this) {
      var obj = YAHOO.util.Connect.asyncRequest('POST', url, callback);

      }
      },

      handleSuccess: function(o) {
      //success code

      },

      handleFailure: function(o) {
      //failure code

      },

      callback: {

      success: this.handleSuccess,

      failure: this.handleFailure,

      timeout: 5000,

      argument: {}

      }

      }
    • randomjuju
      Have you verified that the request actually goes out to the server? My first guess is that the request is never actually fired, but I can t say too much about
      Message 2 of 7 , Jan 3, 2007
        Have you verified that the request actually goes out to the server?
        My first guess is that the request is never actually fired, but I
        can't say too much about that based on the code you've provided.

        If I had to guess at something weirder, I'd say that "this" in the
        "callback" object is referring to the callback object itself (rather
        than to the instance of MyClass), although I don't feel confident
        that's what's happening. A quick-and-dirty way to test that
        hypothesis would be to do something like "alert(callback.success);"
        just before sending off the asyncRequest.

        -Jon

        --- In ydn-javascript@yahoogroups.com, "Sagar Dahiya"
        <sagardahiya@...> wrote:
        >
        > --- In ydn-javascript@yahoogroups.com, "Thomas S. Sha" <tsha@> wrote:
        > >
        > > --- In ydn-javascript@yahoogroups.com, "Sagar Dahiya"
        > > <sagardahiya@> wrote:
        > > >
        > > > Hi,
        > > >
        > > > I am using a class, which defines a callback, as a prototype for
        that
        > > > class. For success and failure in the callback, i'm calling some
        other
        > > > prototype functions of that class(eg success: this.handleSuddess).
        > > > This isn't working out for me and it doesnt even give any error
        > > > message(The function is not even getting called). Can somebody
        help me
        > > > out with this problem?
        > > >
        > > > Thanks,
        > > > Sagar
        > >
        > > Can you post your functional script so I can see what's happening?
        > >
        > > Regards,
        > > Thomas
        > >
        >
        > The code is something like this:
        >
        > MyClass = function(url) {
        > this.url = url;
        > }
        >
        > MyClass.prototype = {
        >
        > changed: function() {
        > with(this) {
        > var obj = YAHOO.util.Connect.asyncRequest('POST', url, callback);
        >
        > }
        > },
        >
        > handleSuccess: function(o) {
        > //success code
        >
        > },
        >
        > handleFailure: function(o) {
        > //failure code
        >
        > },
        >
        > callback: {
        >
        > success: this.handleSuccess,
        >
        > failure: this.handleFailure,
        >
        > timeout: 5000,
        >
        > argument: {}
        >
        > }
        >
        > }
        >
      • Sagar Dahiya
        Yes, the request is going out fine. I tried to put the alert, but it says undefined , probably this refers to some other object. Is there any work-around
        Message 3 of 7 , Jan 3, 2007
          Yes, the request is going out fine. I tried to put the alert, but it
          says "undefined", probably "this" refers to some other object. Is
          there any work-around for accessing object of MyClass on success of
          callback??

          --- In ydn-javascript@yahoogroups.com, "randomjuju" <jon.chambers@...>
          wrote:
          >
          > Have you verified that the request actually goes out to the server?
          > My first guess is that the request is never actually fired, but I
          > can't say too much about that based on the code you've provided.
          >
          > If I had to guess at something weirder, I'd say that "this" in the
          > "callback" object is referring to the callback object itself (rather
          > than to the instance of MyClass), although I don't feel confident
          > that's what's happening. A quick-and-dirty way to test that
          > hypothesis would be to do something like "alert(callback.success);"
          > just before sending off the asyncRequest.
          >
          > -Jon
          >
          > --- In ydn-javascript@yahoogroups.com, "Sagar Dahiya"
          > <sagardahiya@> wrote:
          > >
          > > --- In ydn-javascript@yahoogroups.com, "Thomas S. Sha" <tsha@> wrote:
          > > >
          > > > --- In ydn-javascript@yahoogroups.com, "Sagar Dahiya"
          > > > <sagardahiya@> wrote:
          > > > >
          > > > > Hi,
          > > > >
          > > > > I am using a class, which defines a callback, as a prototype for
          > that
          > > > > class. For success and failure in the callback, i'm calling some
          > other
          > > > > prototype functions of that class(eg success: this.handleSuddess).
          > > > > This isn't working out for me and it doesnt even give any error
          > > > > message(The function is not even getting called). Can somebody
          > help me
          > > > > out with this problem?
          > > > >
          > > > > Thanks,
          > > > > Sagar
          > > >
          > > > Can you post your functional script so I can see what's happening?
          > > >
          > > > Regards,
          > > > Thomas
          > > >
          > >
          > > The code is something like this:
          > >
          > > MyClass = function(url) {
          > > this.url = url;
          > > }
          > >
          > > MyClass.prototype = {
          > >
          > > changed: function() {
          > > with(this) {
          > > var obj = YAHOO.util.Connect.asyncRequest('POST', url, callback);
          > >
          > > }
          > > },
          > >
          > > handleSuccess: function(o) {
          > > //success code
          > >
          > > },
          > >
          > > handleFailure: function(o) {
          > > //failure code
          > >
          > > },
          > >
          > > callback: {
          > >
          > > success: this.handleSuccess,
          > >
          > > failure: this.handleFailure,
          > >
          > > timeout: 5000,
          > >
          > > argument: {}
          > >
          > > }
          > >
          > > }
          > >
          >
        • randomjuju
          Well, I ve had luck defining the callback object within a function s scope. That is, I build the callback object (still making references to this ) inside
          Message 4 of 7 , Jan 4, 2007
            Well, I've had luck defining the callback object within a function's
            scope. That is, I build the callback object (still making references
            to "this") inside the same function that actually kicks off the async
            request. The difference seems to be that you're defining your
            callback object in the scope of a prototype object, while the cases
            I've been successful with have happened in the scope of a member function.

            Now, to be fair, I'm not completely sure why "this" is misbehaving
            inside of a prototype object. Here's my best guess, though:

            When you call a method in a non-static way (is there a name for
            that?), "this" refers to that method's parent object. In all other
            cases, "this" will take on a different meaning (I believe it will take
            on a reference to the nearest "this"-able object in the call stack,
            but I'm not entirely sure). My guess is that, since the prototype
            object is interpreted and evaluated by the global object, that's what
            "this" is referring to. I'm willing to bet that "this" in the
            callback refers to the "window" object. To test the hypothesis, I'd
            add another property to the callback object like this:

            callback = {
            ...,
            test: this.location
            };

            ...then I'd "alert(callback.test)" and see if it's defined/meaningful.
            If so, I'd say we've found our culprit.

            This is a really fascinating and pretty deep problem; I'm relly
            tempted to write more about it, but I feel like I'm having a hard
            enough time explaining this clearly as it is. Does any of this help
            (or even make sense)?

            Thanks!

            -Jon

            --- In ydn-javascript@yahoogroups.com, "Sagar Dahiya"
            <sagardahiya@...> wrote:
            >
            > Yes, the request is going out fine. I tried to put the alert, but it
            > says "undefined", probably "this" refers to some other object. Is
            > there any work-around for accessing object of MyClass on success of
            > callback??
            >
            > --- In ydn-javascript@yahoogroups.com, "randomjuju" <jon.chambers@>
            > wrote:
            > >
            > > Have you verified that the request actually goes out to the server?
            > > My first guess is that the request is never actually fired, but I
            > > can't say too much about that based on the code you've provided.
            > >
            > > If I had to guess at something weirder, I'd say that "this" in the
            > > "callback" object is referring to the callback object itself (rather
            > > than to the instance of MyClass), although I don't feel confident
            > > that's what's happening. A quick-and-dirty way to test that
            > > hypothesis would be to do something like "alert(callback.success);"
            > > just before sending off the asyncRequest.
            > >
            > > -Jon
            > >
            > > --- In ydn-javascript@yahoogroups.com, "Sagar Dahiya"
            > > <sagardahiya@> wrote:
            > > >
            > > > --- In ydn-javascript@yahoogroups.com, "Thomas S. Sha" <tsha@>
            wrote:
            > > > >
            > > > > --- In ydn-javascript@yahoogroups.com, "Sagar Dahiya"
            > > > > <sagardahiya@> wrote:
            > > > > >
            > > > > > Hi,
            > > > > >
            > > > > > I am using a class, which defines a callback, as a prototype for
            > > that
            > > > > > class. For success and failure in the callback, i'm calling some
            > > other
            > > > > > prototype functions of that class(eg success:
            this.handleSuddess).
            > > > > > This isn't working out for me and it doesnt even give any error
            > > > > > message(The function is not even getting called). Can somebody
            > > help me
            > > > > > out with this problem?
            > > > > >
            > > > > > Thanks,
            > > > > > Sagar
            > > > >
            > > > > Can you post your functional script so I can see what's happening?
            > > > >
            > > > > Regards,
            > > > > Thomas
            > > > >
            > > >
            > > > The code is something like this:
            > > >
            > > > MyClass = function(url) {
            > > > this.url = url;
            > > > }
            > > >
            > > > MyClass.prototype = {
            > > >
            > > > changed: function() {
            > > > with(this) {
            > > > var obj = YAHOO.util.Connect.asyncRequest('POST', url, callback);
            > > >
            > > > }
            > > > },
            > > >
            > > > handleSuccess: function(o) {
            > > > //success code
            > > >
            > > > },
            > > >
            > > > handleFailure: function(o) {
            > > > //failure code
            > > >
            > > > },
            > > >
            > > > callback: {
            > > >
            > > > success: this.handleSuccess,
            > > >
            > > > failure: this.handleFailure,
            > > >
            > > > timeout: 5000,
            > > >
            > > > argument: {}
            > > >
            > > > }
            > > >
            > > > }
            > > >
            > >
            >
          • Thomas S. Sha
            ... Your usage of *this* within the callback members falls out of scope; if you introspect *this* in your original callback, you ll notice it value is
            Message 5 of 7 , Jan 5, 2007
              --- In ydn-javascript@yahoogroups.com, "Sagar Dahiya"
              <sagardahiya@...> wrote:
              >
              > Yes, the request is going out fine. I tried to put the alert, but it
              > says "undefined", probably "this" refers to some other object. Is
              > there any work-around for accessing object of MyClass on success of
              > callback??

              Your usage of *this* within the callback members falls out of scope;
              if you introspect *this* in your original callback, you'll notice it
              value is Object[Window]. Write your success and failure handlers
              inline to avoid this condition.

              Example:

              MyClass.prototype = {

              callback: {
              success: function(o){ //success logic },
              failure: function(o){ //failure logic },
              timeout: 5000
              },

              changed: function() {
              with(this) {
              var obj = YAHOO.util.Connect.asyncRequest(
              'POST',
              url,
              callback);
              }
              }
              };

              Regards,
              Thomas
            Your message has been successfully submitted and would be delivered to recipients shortly.