简体   繁体   English

为什么我的方法不返回对象

[英]Why is my method not returning the object

My ajax response is correct but i am unable to return the response object back to my method call. 我的ajax响应是正确的,但是我无法将响应对象返回到我的方法调用中。 The problem lies in the first part of the if statement. 问题出在if语句的第一部分。 This is my first project with attempts at OOP. 这是我的第一个尝试OOP的项目。 Please let me know how to fix this and any other OOP code suggestions are very welcome. 请让我知道如何解决此问题,非常欢迎其他任何OOP代码建议。

$("#search-btn").on('click', function(e) {
        e.preventDefault();
        searchTerm = $("#search-input").val();
        var params = {'q': searchTerm, 'limit': 13};
        var url = "https://api.twitch.tv/kraken/search/" + searchType + "?";

/*===*/     if(searchType === "games") {
            url = "https://api.twitch.tv/kraken/search/games?type=suggest";
            var gamesSearch = new TwitchApiCall(searchType, searchTerm, params, url);
            var data = gamesSearch.apiCall();//How do i get response from api call here?=====
            console.log(data);//returns undefined================   /*===*/

        //will change these when above works=====================
        } else if (!searchType || searchType === "channels") {
            var defaultSearch = new TwitchApiCall(searchType, searchTerm, params, url);
            defaultSearch.apiCall();
            displaySearchResults(defaultSearch.response.channels);

        } else {
            var streamSearch = new TwitchApiCall(searchType, searchTerm, params, url);
            streamSearch.apiCall();
            displaySearchResults(streamSearch.response.streams);
        }
    });
}

function TwitchApiCall(searchType, searchTerm, params, url) {
    this.params = params;
    this.url = url;

    this.apiCall = function() {
        $.ajax({
            url: this.url,
            data: this.params,

/*===*/     success: function (response) {

                next = response._links.next;
                prev = response._links.prev;
                console.log(response);//returns the object i want==================
            }
        });        /*===*/

        $("#page-title").html("Twitch.tv/" + searchType + "/" + searchTerm);
        $("#sort-pop").addClass('active');
    };
}

Thanks in advance 提前致谢

Part of the problem is that this problem might well not be a good fit for the type of OOP you're trying. 问题的一部分是,此问题可能不太适合您尝试的OOP类型。 Or at least there is probably little reason to make an object out of that Twitch thingy. 或至少没有什么理由用Twitch制成物体。

This is untested, but looks like it might do it, and is much cleaner code: 这未经测试,但看起来可能会完成,并且代码更简洁:

$("#search-btn").on('click', function(e) {
    e.preventDefault();
    searchTerm = $("#search-input").val();
    var params = {'q': searchTerm, 'limit': 13};
    var url = "https://api.twitch.tv/kraken/search/" + searchType + "?";

    if (searchType === "games") {
        url = "https://api.twitch.tv/kraken/search/games?type=suggest";
        callTwitch(searchType, searchTerm, params, url, function(data) {
            console.log(data);
        })
    } else if (!searchType || searchType === "channels") {
        callTwitch(searchType, searchTerm, params, url, function(data) {
            displaySearchResults(data.channels);
        });
    } else {
        callTwitch(searchType, searchTerm, params, url, function(data) {
            displaySearchResults(data.streams);
        });
    }
});


function callTwitch(searchType, searchTerm, params, url, callback) {
    $.ajax({
        url: url,
        data: params,
        success: function (response) {
            next = response._links.next;
            prev = response._links.prev;
            callback(response);
        }
    });

    $("#page-title").html("Twitch.tv/" + searchType + "/" + searchTerm);
    $("#sort-pop").addClass('active');
}

It is missing error-handling. 它缺少错误处理。 What happens if the AJAX call has bad data? 如果AJAX呼叫的数据不正确怎么办? What if it doesn't return? 如果不返回怎么办? But it should get you going. 但这应该可以帮助您前进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM