views:

36

answers:

2

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!

A: 

I have done something similar in my app since I am using the Facebook JavaScript SDK and Twitter SDK and want to provide a consistent interface to access both. So I namespace the variables under App. For twitter anywhere, this is how the variable is captured.

window.App = {};

twttr.anywhere(function(T) {
    App.Twitter = {
        getValue: function(property) {
            return T.currentUser.data(property);
        },
        getPublicTimeline: function() {
            return T.Status.publicTimelime();
        }
    };
});

We are calling the anywhere function and passing it a callback function. The callback function is needed because the anywhere library might not be loaded at this point. By passing the entire function, we are saying that this function should be executed whenever the anywhere library is loaded.

Now when the library does load, this function will execute, define the App.Twitter property which contains a getValue function. The anywhere or T object is captured in the closure.

If you now call,

App.Twitter.getValue("screen_name");

the actually anywhere object (T), will be used to get the screen_name property.

Anurag
Do note that the calls used above will not work with the actual library, and are there just for demonstration.
Anurag
thanks, but that doesn't quite work. I realized the problem is simply that I need to add a callback that executes when twitter is done processing. I got it working, thanks.
devadvocate
A: 

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.

devadvocate