Results 1 to 4 of 4
  1.    #1  
    Just thought I'd share something that may help newbies...

    A programming pet peeve of mine is not to use identical code in multiple places. Instead, it should be placed in a function or class somewhere. I was helping someone with an app earlier and noticed that she reused the same code in multiple assistants whenever it involved "this" (ie. this.controller). I asked her why and she said that she couldn't figure out how to make functions in other assistants (namely app and stage) point to the correct instance of "this", instead it would look at it's own. The solution is very simple, just pass "this" as an additional argument to your function. A simple example...

    A common task in webOS is to start/stop a scrim with a spinner. First, add the required code to your view file and setup the spinner in the setup() function of your assistant:

    Code:
    // IN VIEW FILE
    
      <div id="scrim" class="palm-scrim">
        <div id="spinner" x-mojo-element="Spinner"></div>
      </div>
    
    // IN ASSISTANT SETUP FUNCTION
    
      this.spinnerModel = {spinning: false};
      this.controller.setupWidget('spinner', {spinnerSize: 'large'}, this.spinnerModel);
    Next, create a function (with a namespace) in your app or stage controller's instantiation function that will accept two arguments...action and that. "that" is what will hold "this" from the target assistant:

    Code:
      function AppAssistant (appController)
      {
          myNamespace.scrimSpinner = function(action, that)
          {
              if (action == 'start')
              {
                  that.controller.get('scrim').show();
                  that.spinnerModel.spinning = true;
                  that.controller.modelChanged(that.spinnerModel, that);
              }
              else if (action == 'stop')
              {
                  that.controller.get('scrim').hide();
                  that.spinnerModel.spinning = false;
                  that.controller.modelChanged(that.spinnerModel, that);
              }
          }
      }
    Finally, use the function in any assistant that has the scrim and spinner setup. Literally pass the word "this" as your "that" argument...

    Code:
    myNamespace.scrimSpinner('start', this);
    myNamespace.scrimSpinner('stop', this);
    Now you've cut out a few dozen lines of unnecessary code in your app! Create as many functions as you can with this method (if used multiple times throughout your app).
    Last edited by Laxidasical; 03/09/2010 at 12:47 PM. Reason: Typo
    MoBill - Use your Authorize.net account to bill your customers with your webOS device!!
    MoJack - Track your lost or stolen webOS device from anywhere!
    Time to get VIRAL
  2. #2  
    Thanks Lax!

    I just wish I had "this" (horrible pun intended) tutorial a good 6 months ago when I was still struggling with all my code.

    I figured it out the hardway, but that's definitely a very valid point and a difficult one to grasp if you're not coming from an Object Oriented background.
  3.    #3  
    It actually took me a bit of time to figure this out as well when I was coding my first app. When I did I figured I was just being slow to miss such a simple thing (something I find myself doing way too often!), but after chatting with her I see that it's something that people may easily overlook...
    MoBill - Use your Authorize.net account to bill your customers with your webOS device!!
    MoJack - Track your lost or stolen webOS device from anywhere!
    Time to get VIRAL
  4. #4  
    I still don't properly understand 'this'. There's a video out on youtube from Douglas Crawford that supposedly explains it, but haven't had time to watch it. Or study your code Lax. I should probably make that a goal before I start in on the next app.
    Author of Exch-Calc, Dog Years, Cat Years and all future creations of Pre-zy Software. Available in the Application Catalog.

Posting Permissions