如何使用闭包访问 Twitter @Anywhere javascript api?

发布于 2024-09-17 06:22:52 字数 937 浏览 4 评论 0原文

我想做一些在经典的面向对象语言(如 Java、C# 等)中很容易做到的事情。我只是想访问实例化对象的属性。该对象在浏览器的窗口对象中具有全局作用域,并由 twitter @anywhere API 提供。

对于我的代码示例,假设您已经登录用户。

例如,如果我使用 java,我会说(假设所有字段都是公共的:

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

出于某种原因,这在 Javascript 中很难。我已经找到了一种解决方法 ,然后将其取出,但这很丑陋,但我只想直接访问它。

在 twitter Anywhere API 使用的匿名方法中,我将想要的值设置为 DOM元素 它甚至没有通过 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);

帮忙!为什么 Javascript 这么难使用?我想使用闭包。

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

简美 2024-09-24 06:22:52

自从我使用 Facebook JavaScript SDK 和 Twitter SDK 并且希望提供一致的接口来访问这两者以来,我在我的应用程序中做了类似的事情。因此,我在 App 下为变量命名。对于任何地方的 Twitter,这就是捕获变量的方式。

window.App = {};

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

我们正在调用 anywhere 函数并向其传递一个回调函数。需要回调函数是因为此时可能尚未加载 Anywhere 库。通过传递整个函数,我们是说只要加载 Anywhere 库,就应该执行该函数。

现在,当库加载时,该函数将执行,定义包含 getValue 函数的 App.Twitter 属性。任何地方或 T 对象都会在闭包中捕获。

如果您现在调用,

App.Twitter.getValue("screen_name");

则实际的任意位置对象 (T) 将用于获取 screen_name 属性。

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.

孤单情人 2024-09-24 06:22:52

这就是我需要做的。

document.getElementById('messagePanel').innerHTML = "loading...";
                window.twttr.anywhere(function(T)
                {

                    document.getElementById('messagePanel').innerHTML = "screen_name: " + T.currentUser.data('screen_name');
                });

这让我意识到我的问题只是当 Twitter 从异步调用返回时我必须使用回调。这帮助我解决了如何将其包装为 gwt 的最初问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文