Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: widget.system asynchronous use from javascript object not working



2006/8/29, Nate Heagy <email@hidden>:
 - first, the second argument to widget.system is a reference to a function, not an actual function call. Leave off the brackets, (), to pass the function as an argument. Currently you are passing it the result of the function which is being called immediately.

 Didn't realize that I referenced a call when I should have referenced an object.

 - second, when using an asyncronous call the results of the call are passed as an argument to the function called, so _temporarySystemCall is not needed.

 Thanks for clearing that up, I didn't know/understand that either.

 - third, since this is a method of an object you will want to pass it a bound reference to the method since _javascript_ has a nasty habit of changing the definition of 'this'. I prefer to use bind() from prototype.js since it is simple and licensed such that it can be recopied at will.

I have noticed that 'this' sometimes is more like 'that'... I have created a workaround for XMLHttpRequest but I did not really realize what I worked around I guess. It might have been because of a redefinition of 'this'.

If you'd like to take my advice then your call will become this:

widget.system ("/usr/bin/id -un", this._temporaryGetUserName.bind(this));

Hope that helps,

N

It did help and the widget is working now :-)

I acctually tried both with and without brackets on the call to widget.system before but both crashed. I think I understand why now.
The reason it did crash without brackets was probably because _javascript_ redefined 'this'. Using bind() fixed this.
Also I got an undefined value when I used brackets because (as Glenn Andreas pointed out in a mail that I think only came to me) when using brackets, the command would be run once before the widget.system call. And if it runs before widget.system it will of course don't have anything to work with.

Thanks for all your help!

Gustav


On 8/29/06, Gustav Axelsson <email@hidden> wrote:
I'm trying to call widget.system asynchronously from a _javascript_ object but I cannot get it to work properly.
It does work if I specify null instead of an endhandler but since we are not supposed to use null anymore I would like to do it "the proper way" even from within an object.

Here is the problematic section of my source code (_temporaryGetUserName crashes the widget):

function XmltvBack() {
    this._userID = "";
    this._temporarySystemCall;

    this._doneButton = new AppleGlassButton( document.getElementById("backDoneButton"),"Done",this.doneButtonPressed());
    this._usersChannels = new Array();
    this._usersChannelsHash = new Array();
    this._channelsXmlIsReady = false;
    this._channelsXml;
   
    this._temporaryInstaller();
   
}

var globalXmltvPath;

XmltvBack.prototype._temporaryInstaller = function(){
    document.getElementById("debug").innerHTML = "_temporaryInstaller körs";
    if(window.widget){
        document.getElementById("debug").innerHTML = "username = "+widget.system("/usr/bin/id -un", null).outputString; // Works!
        this._temporarySystemCall = widget.system("/usr/bin/id -un", this._temporaryGetUserName()); // Does start the endHandler but this._temporarySystemCall is undefined - why? :-(
    } else
        this._temporaryGetUserName();
}

XmltvBack.prototype._temporaryGetUserName = function(){
    document.getElementById("debug").innerHTML = "_temporaryGetUserName running";
    if(window.widget){
        document.getElementById ("debug").innerHTML = "_temporaryGetUserName is about to save this._userID";
        this._userID = ""+this._temporarySystemCall.outputString; // Row 29. Crashes the widget since this._temporarySystemCall is undefined
        document.getElementById("debug").innerHTML = "userID= "+this._userID;
    } else {
        this._userID = "gusax840";
        alert("_continueStartup running");
    }
    globalXmltvPath = "/Users/"+this._userID+"/Library/Xmltv/";
    this._channelsXml = new XmltvChannelsXml(globalXmltvPath+"server/channels.xml",this);
}

I've attached the widget as a zip file. The above code can be found in _javascript_/XmltvBack.js starting on row 1. Please note that the widget will crash on row 29 if you open it in Dashboard. It works in Safari though, if you want to see how it should look like.

Gustav


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Dashboard-dev mailing list      ( email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/dashboard-dev/email@hidden

This email sent to email@hidden





--
Nate
  -------
heagy.com

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Dashboard-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/dashboard-dev/email@hidden

This email sent to email@hidden

References: 
 >widget.system asynchronous use from javascript object not working (From: "Gustav Axelsson" <email@hidden>)
 >Re: widget.system asynchronous use from javascript object not working (From: "Nate Heagy" <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.