Search code examples
javascripttwitterclosurestwitter-anywhere

How do I use closures to access twitters @Anywhere javascript api?


I want to do something that in a classical object oriented language like Java, C# etc. is very easy to do. I simply want to access a property of an instantiated object. The object is globally scoped in the browser's window object, and provided by the twitter @anywhere API.

For my code examples, assume you have already logged the user in.

If I were using java for instance, I would say (assuming all fields were public:

twttr = new twtter();
String screenName = twtter.currentUser.data('screen_name');

For some reason, this is way hard in Javascript. I've gotten a workaround working where inside the anonymous method that the twitter anywhere API is using, I set the value I want to a DOM element, and fish it out later. This is ugly though. I just want to access it directly.

Here's what I have so far, which doesn't even pass syntax checks in eclipse:

function AnywhereFacade()
{
    var twitterReference;
    window.twttr.anywhere
    (
        return function(T)
        {
            twitterReference = T;
        };
    )
    getValue(propertyToGet)
    {
        return twitterReference.currentUser.data(propertyToGet);
    }
};

var anywhereFacade = AnywhereFacade();

var screen_name = anywhereFacade.getValue("screen_name");

alert("screen name is: " + propertyGetter);

Please help! Why is Javascript so hard to use anyway? What I'm trying to do is use a closure I think.

Thanks!


Solution

  • this is all I needed to do.

    document.getElementById('messagePanel').innerHTML = "loading...";
                    window.twttr.anywhere(function(T)
                    {
    
                        document.getElementById('messagePanel').innerHTML = "screen_name: " + T.currentUser.data('screen_name');
                    });
    

    this made me realize my issue was just that I had to use a callback for when twitter returned from the async call. that helped me solve my initial problem of how to wrap it for gwt.