从 JSONP 回调返回数据

发布于 2024-12-15 04:25:01 字数 687 浏览 1 评论 0原文

我有一个获取 jsonp 数据的类。我想简单地返回一个值,但无法这样做。回调不会更新类属性 - 不确定这是时间问题还是范围问题

Ext.define("App.ContentManager", {
    extend: "Ext.util.Observable",
    singleton: true,

    data: 'empty',
    getData: function() {
        this.doLoad();
        console.log(a.data) //not correct - shows original value
        return this.data;

    },
    doLoad: function(url) {
        var a = this;
        Ext.util.JSONP.request({
            url: "http://example.com/somejson",
            callbackKey: "callback",
            callback: function(c) {
                a.data = c;
                console.log(a.data) //correct json data is here
            }
        })
    },
});

I have a class that fetches jsonp data. I would like to simply return a value but am unable to do so. The callback does not update the class property - not sure if it is a timing or scope issue

Ext.define("App.ContentManager", {
    extend: "Ext.util.Observable",
    singleton: true,

    data: 'empty',
    getData: function() {
        this.doLoad();
        console.log(a.data) //not correct - shows original value
        return this.data;

    },
    doLoad: function(url) {
        var a = this;
        Ext.util.JSONP.request({
            url: "http://example.com/somejson",
            callbackKey: "callback",
            callback: function(c) {
                a.data = c;
                console.log(a.data) //correct json data is here
            }
        })
    },
});

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

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

发布评论

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

评论(2

风尘浪孓 2024-12-22 04:25:01

当然是时间问题。使用 getDate 函数中的 doLoad(),您只需启动请求,而无需等待它完成才能在下一行中准备好数据。您只需在 doLoad 方法的 callback: function 中准备好数据。因此,无论您需要做什么,最好在一个函数中发送请求,然后在 callback:function 中设置一个将使用返回数据的函数。

更新

另外我刚刚注意到还有一个范围问题。您可以通过添加 a 在 JSONP 请求中传递范围

          url: "http://example.com/somejson",
        scope: this,    //this line
  callbackKey: "callback",

,然后在 getDate 中您应该: this.data 因为 adoLoad方法。

Of course is timing issue. With doLoad() in the getDate function you just start the request and you don't wait for it to complete in order to have the data ready in the next line. You only have the data ready in the callback: function of the doLoad method. So whatever you need to do it's best to send the request in one function and then in the callback:function have a function that will use the data returned.

Update

Also I just noticed is a scope issue too. You shout pass the scope in the JSONP request with adding a

          url: "http://example.com/somejson",
        scope: this,    //this line
  callbackKey: "callback",

And then in the getDate you should: this.data since a is local variable in the doLoad method.

谷夏 2024-12-22 04:25:01

正如现有注释中所述,由于 doLoad 方法异步检索数据,因此无法直接从方法调用返回结果。您的两个选择是:

  • 使用回调参数定义您的方法(这通常在 Ext 本身中完成),以便在 JSONP 回调中执行传递的回调函数,传递返回的数据。
  • 定义您的类可以从 JSONP 回调触发的自定义事件,并将数据作为事件参数传递。这更适合多个组件可能对 JSONP 调用的结果感兴趣的情况。

或者你甚至可以两者都做(未经测试):

doLoad: function(options) {
    var me = this;

    Ext.util.JSONP.request({
        url: options.url,
        scope: me,
        callbackKey: "callback",

        callback: function(data) {
            me.fireEvent('dataloaded', me, data);
            if (options.callback) {
                options.callback.apply(options.scope || me, [data]);
            }
        }
    })
}

As noted in existing comments, since the doLoad method retrieves the data asynchronously, there is no way to return the result directly from the method call. Your two options would be:

  • Define your method with a callback parameter (this is done commonly in Ext itself) so that inside the JSONP callback you would execute the passed callback function, passing the returned data.
  • Define a custom event that your class could fire from the JSONP callback, passing the data as an event argument. This would be more suitable in the case where multiple components might be interested in the result of the JSONP call.

Or you could even do both (untested):

doLoad: function(options) {
    var me = this;

    Ext.util.JSONP.request({
        url: options.url,
        scope: me,
        callbackKey: "callback",

        callback: function(data) {
            me.fireEvent('dataloaded', me, data);
            if (options.callback) {
                options.callback.apply(options.scope || me, [data]);
            }
        }
    })
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文