jQuery 中 getJSON 的范围

发布于 2024-11-28 00:38:47 字数 913 浏览 0 评论 0原文

使用 getJSON 时,我在尝试管理范围时遇到了一些麻烦。
因此,在 HTML 页面上,我有一个无序列表,需要填充 JSON 文件中的列表项。该列表是轮播的标记。

HTML:

JS:

grabJSON : function() {
    $.getJSON('file.json', function(data) {
        var items = [];
        $.each(data.foo, function(k, v) {
            items.push('<li>' + v.bar + '</li>');
        }
        $('ul.carousel').append(items.join(''));
        // carousel action here
        $('ul.carousel').carouselFunction({
            // carousel options here
        });
    });
}

目前,我必须将 carousel 函数放在 getJSON 函数中。如果我创建另一个函数来设置轮播,我就会失去 getJSON 中的范围。

解决此问题的首选方法是什么,以便我可以拥有从 getJSON 调用的 setupCarousel : function() ?通常,如果我想从对象中的另一个函数调用函数,我可以直接使用 this.setupCarousel() ,但是对于嵌套作用域,我不确定如何处理这个问题。

此外,在 getJSON 函数之外,我无权访问附加的任何元素。因此我可以访问 ul,但不能访问从 getJSON 添加列表项时创建的任何列表项。

I'm having a bit of trouble trying to manage scope when using getJSON.
So on a HTML page I have an unordered list to be filled with list items from a JSON file. The list is markup for a carousel.

HTML:
<ul class="carousel"></ul>

JS:

grabJSON : function() {
    $.getJSON('file.json', function(data) {
        var items = [];
        $.each(data.foo, function(k, v) {
            items.push('<li>' + v.bar + '</li>');
        }
        $('ul.carousel').append(items.join(''));
        // carousel action here
        $('ul.carousel').carouselFunction({
            // carousel options here
        });
    });
}

Currently, I have to place the carousel function within the getJSON function. If I make another function for setting up the carousel, I lose the scope within getJSON.

What is a preferred method of breaking this out so I can have a setupCarousel : function() that is called from getJSON? Normally if I want to call a function from another function in an object I can just go this.setupCarousel(), however with nested scope I'm not sure how to handle this.

Also, outside of the getJSON function I do not have access to any of the elements that were appended. So I can access ul, but not any of the list items created while adding them from getJSON.

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

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

发布评论

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

评论(2

一念一轮回 2024-12-05 00:38:47

$.getJSON 调用是异步的。因此,如果您这样做:

thing.grabJSON();
do_some_other_stuff();

do_some_other_stuff 将在 $.getJSON 调用完成之前执行。您必须将所有内容放入 $.getJSON 回调中,因为该代码段将在将来的某个时间执行;回调可以自由地将其接收到的任何数据传递给其他函数(如 Kishnore 的答案),但在 $.getJSON 之后执行的代码(其中 after 表示源代码中之后code) 不能依赖于 $.getJSON 调用来执行任何操作。

您可以这样做:

function buildCarousel(items) {
    $('ul.carousel').append(items.join(''));
    $('ul.carousel').carouselFunction({
        // carousel options here
    });
}

// ...

grabJSON : function(cb) {
    $.getJSON('file.json', function(data) {
        var items = [];
        $.each(data.foo, function(k, v) {
            items.push('<li>' + v.bar + '</li>');
        }
        cb(items);
    });
}

//...

thing.grabJSON(buildCarousel);

如果您想将 $.getJSON 与需要对该 JSON 执行的操作分开。在这种特殊情况下,像上面那样将其分解是比实际工作更繁忙的工作,但在处理 AJAX 时,这种模式(回调中的回调,一直向下的回调)非常常见。

The $.getJSON call is asynchronous. So, if you do this:

thing.grabJSON();
do_some_other_stuff();

The do_some_other_stuff will be executed before the $.getJSON call finishes. You have to put everything inside the $.getJSON callback because that piece of code will be executed at some time in the future; the callback is free to pass any data it receives to other functions (as in Kishnore's answer) but code that is executed after $.getJSON (where after means after in the source code) can't depend on the $.getJSON call doing anything.

You could do something like this:

function buildCarousel(items) {
    $('ul.carousel').append(items.join(''));
    $('ul.carousel').carouselFunction({
        // carousel options here
    });
}

// ...

grabJSON : function(cb) {
    $.getJSON('file.json', function(data) {
        var items = [];
        $.each(data.foo, function(k, v) {
            items.push('<li>' + v.bar + '</li>');
        }
        cb(items);
    });
}

//...

thing.grabJSON(buildCarousel);

if you wanted to separate the $.getJSON from what needs to be done with that JSON. In this particular case, breaking it apart like the above is more busy work than real work but that sort of pattern (callbacks within callbacks, callbacks all the way down) is pretty common when dealing with AJAX.

王权女流氓 2024-12-05 00:38:47

你尝试过这样的事情吗?

grabJSON : function() {
    $.getJSON('file.json', function(data) {
        var items = [];
        $.each(data.foo, function(k, v) {
            items.push('<li>' + v.bar + '</li>');
        }
        makeCarousel(items);
    });
}
//pass items to make carosuel function and use it this should solve your problem.
makeCarosuel: function(items) {
$('ul.carousel').append(items.join(''));
        // carousel action here
        $('ul.carousel').carouselFunction({
            // carousel options here
        });
}

Did you Try something like this?

grabJSON : function() {
    $.getJSON('file.json', function(data) {
        var items = [];
        $.each(data.foo, function(k, v) {
            items.push('<li>' + v.bar + '</li>');
        }
        makeCarousel(items);
    });
}
//pass items to make carosuel function and use it this should solve your problem.
makeCarosuel: function(items) {
$('ul.carousel').append(items.join(''));
        // carousel action here
        $('ul.carousel').carouselFunction({
            // carousel options here
        });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文