title: jQuery-ui widget factory, event handlers and that
body: <p>I've been developing a few jQuery widgets recently but 2 things have been bothering me and so I'm coming to you hoping you'll have a nicer way to do it.</p>
  
  <p>1) I quite like the fact of using "that" instead of this. Makes the code much clearer and avoid quite a lot of bugs. To make it simple I always use "that" as this of the widget.
  However I don't know how to make my "that" global so what I do is:</p>
  
  <pre><code>$.widget("widgetName", {
      method1: function() {
          var that = this;
      },
      method2: function() {
          var that = this;
      }
  });
  </code></pre>
  
  <p>As you can see that's heavy in code and not much better. I was wondering if i was valid to do:</p>
  
  <pre><code> var that = $.widget("widgetName", {
       method1: function() {
           //do something
       },
       method2: function() {
           //do something
           that.method1();
       }
    });
  </code></pre>
  
  <p>Or will that pose any problem? If that is impossible how do you think is the best way to do it?</p>
  
  <p>2) This is really linked to my first question and an answer to it should be enough:
  For my event handlers I often need to use my "that" to call methods for eg. So what I currently do is</p>
  
  <pre><code> $.widget("widgetName", {
       _handlers: {},
       _create: function() {
           var that = this;
           that._handlers: {
               handler1: function(e) { 
                   //do something
                   that.method();
               } //...
           };
           that.on("event", that._handlers.handler1);
       },
       method: function() {}
   });
  </code></pre>
  
  <p>Do you see a nicer way I could do this? My big need is to be able to move the whole initialization of that._handlers out of that._create </p>
  
  <p>These are quite open questions. I'm really trying to find a way to make my jquery widget really clear and maintainable and I'm curious to know how people do.</p>
  
  <p>Thanks a lot for your opinion on this. </p>