1 Reply Latest reply on Apr 19, 2008 1:58 PM by schlegel

    Set Javascript callback scope in Seam Remoting

    staticr

      In my Seam application that I am working on I make heavy use of Javascript for the UI; specifically I use ExtJS for the front end.  When I invoke a Seam Component from within a Javascript object instance the scope of that call is lost in the callback.  As an example:


      MyJSClass = {
      
        x: 'Hello',
      
        someFunction: function() {
          this.x = this.x + ', World!';
          Seam.Components
            .getInstance('someComponent')
            .someMethod(arg, this.callback);
        },
      
        callback: function(response) {
          alert(this.x + response);
        }
      
      };



      The scope of this.x does not point to the object from the call.  Is there a way to add an optional argument to the Component's method invocation where we can set the scope?  Someting like:


          Seam.Components
            .getInstance('someComponent')
            .someMethod(arg, this.callback, this);
      



      and have Seam use this scope when invoking the callback?


      Thanks,
        Colin

        • 1. Re: Set Javascript callback scope in Seam Remoting
          schlegel

          Hi


          You have a closure problem. Try it this way:



          MyJSClass = {
          
            x: 'Hello',
          
            someFunction: function() {
              this.x = this.x + ', World!';
              var self = this;
              Seam.Components
                .getInstance('someComponent')
                .someMethod(arg, self.callback);
            },
          
            callback: function(response) {
              alert(this.x + response);
            }
          
          };
          



          in closures you have always problems with the 'this' keyword.