在另一个函数中使用JSON回调

时间:2013-12-22 17:13:26

标签: jquery json callback flickr

我正在尝试使用在另一个函数中的函数中获得的getJSON结果,但我不知道该怎么做,任何人都可以帮助我?

我需要从输入中获取文本并在flickr中查找相关图像,稍后,我必须使用该JSON调用来拍摄这些图片并在我的页面中显示它们。这就是我已经做过的事情:

dataservices.pictureFeedService = function() {

    function searchForPictures(searchFilterEntry, callback) {
        var query = "http://www.flickr.com/services/feeds/photos_public.gne?tags=" + searchFilterEntry + "&format=json&jsoncallback=?";

        $.getJSON(query,callback);
    }

    return {
        searchForPictures: searchForPictures
    };
}();

这是第二个函数,我想使用之前JSON中获得的数据:

dataservices.picturesLayoutService = function() {

    function buildPicturesLayout(divImageContainer) {
        $.getJSON(function(data) {
            //read images url
            // Pop our HTML in the #images DIV
            $("#" + divImageContainer).html(htmlString);
        });
    }
    return {
        buildPicturesLayout: buildPicturesLayout
    };
}();

我用这种方式调用函数:

dataservices.pictureFeedService.searchForPictures(searchTags, dataservices.picturesLayoutService.buildPicturesLayout("images"));

1 个答案:

答案 0 :(得分:0)

尝试类似

的内容
dataservices.picturesLayoutService = function () {
    function buildPicturesLayout(divImageContainer, data) {
        $("#" + divImageContainer).empty();
        //iterate through the items and create a list
        $.each(data.items, function (i, item) {
            $("#" + divImageContainer).append('<img src="' + item.media.m + '" />');
        })
    }
    return {
        buildPicturesLayout: buildPicturesLayout
    };
}();
//write a callback function which will receive the ajax response and pass it to the buildPicturesLayout method
dataservices.pictureFeedService.searchForPictures(searchTags, function (data) {
    dataservices.picturesLayoutService.buildPicturesLayout("images", data)
});