我有一个函数可以向服务器发送请求并处理函数中的响应。然后我想返回响应,但我希望它将它返回到父函数(getChannelId),以便例如getChannelId('someUsername')将返回“someChannelId”:
function getChannelId(username) {
function onSearchResponse(response) {
console.log(response);
var channelId = response.items[0].id;
return channelId; /* This is the value I want to return, but I want to return it within the getChannelId function */
console.log({channelId: channelId });
}
var request = window.gapi.client.youtube.channels.list({
part: 'snippet',
forUsername: username,
});
request.execute(onSearchResponse);
}
如何将响应返回到父函数?
答案 0 :(得分:0)
在getchannelid中,你应该有一个内部函数设置的变量。而不只是返回结果...
var tmp;
function onSearchResponse(....) {
....
tmp = channelId;
}
然后你可以在外部函数中返回tmp。
答案 1 :(得分:0)
通过继续onSearchRespone
回调中的计划来解决此问题。
完整代码:
requirejs.config({
baseUrl: '//cdnjs.cloudflare.com/ajax/libs',
paths: {
jquery: "jquery/2.0.3/jquery.min",
jqueryColor: "jquery-color/2.1.2/jquery.color.min",
annyang: "annyang/1.0.0/annyang.min",
prefixfree: "prefixfree/1.0.7/prefixfree.min",
gapi: "https://apis.google.com/js/client.js?onload=onGAPILoaded",
gjsapi: "http://www.google.com/jsapi?noext",
ytpapi: "https://www.youtube.com/iframe_api?noext"
/*,
ytdapi: ""*/
}
});
var player, Ice;
function shuffle(o) {
for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
};
function onGAPILoaded() {
console.log('Google Client API Loaded, Loading YouTube Data API..');
gapi.client.load('youtube', 'v3', onYTPAPILoaded);
}
function onYTPAPILoaded() {
console.log('YouTube Data API Loaded');
gapi.client.setApiKey('AIzaSyBt4dZrv29ut8U0xeYTRFrgH_rB8zil9_M');
}
function onYouTubeIframeAPIReady() {
player = new YT.Player('video-player', {
height: '720',
width: '1280',
suggestedQuality: 'hd720',
events: {
onStateChange: function(e) {
Ice.state = e.data;
console.log({ STATE: Ice.state });
}
}
});
}
requirejs(['jquery', 'jqueryColor', 'annyang', 'prefixfree', 'gapi', 'ytpapi', 'gjsapi'], function() {
Ice = new ice();
if(annyang) {
var commands = {
'Ice search youtube for *exp': function(param) {
Ice.execute('watchSearchList', param);
},
'Ice watch (youtube) channel': function(param) {
Ice.execute('watchChannel', param);
},
'Ice play': function(param) {
Ice.execute('togglePlayback', param);
},
'Ice fit': function(param) {
Ice.execute('fit', param);
},
'test *exp': Ice.test
};
annyang.init(commands);
annyang.start();
}
function ice() {
/*
* Variables of Ice
*/
this.props = {
order: 'viewCount',
part: 'snippet',
type: 'video',
maxResults: 50,
q: '',
channelId: ''
}
this.state = -1;
this.intelligence = 9001;
/*
* Functions of Ice
*/
/* Debug Functions */
this.execute = function(func, param) {
Ice.flash();
Ice.log(func);
Ice[func](param);
};
this.test = function(term) {
Ice.log(term);
};
this.flash = function() {
var oldColor = $('h1').css('color');
$('h1').animate({ 'color': 'black' }, function() {
$(this).animate({ 'color': oldColor });
});
}
this.log = function(log) {
$('h1 span').text(log);
};
/* Video Functions */
this.togglePlayback = function() {
var func = (Ice.state == 1 ? 'pause':'play')+'Video';
console.log(func);
player[func]();
};
this.fit = function() {
var W = $(window).innerWidth(),
H = $(window).innerHeight();
console.log({ W: W, H: H });
player.setSize(W, H);
$('.video-player').addClass('full').css({
'width': W+'px',
'height': H+'px'1
});
};
this.watchChannel = function() {
Ice.props.channelId = Ice.getChannelId(prompt('Write Channel:', 'EthosLab'));
console.log({ channelId: Ice.props.channelId });
Ice.props.order = 'date';
Ice.playVideos();
}
this.watchSearchList = function(q) {
Ice.props.q = q;
Ice.playVideos();
}
this.playVideos = function() {
function onSearchResponse(response) { showResponse(response); }
function showResponse(response) {
console.log(response);
videoList = [];
for(var i = 0; i < response.items.length; i++) {
videoList.push(response.items[0].id.videoId);
}
// videoList = shuffle(videoList);
console.log({ videoList: videoList });
player.loadPlaylist(videoList);
$('.video-player').animate({
'height': '720px',
'width': '1280px',
});
}
console.log(Ice.props);
var request = window.gapi.client.youtube.search.list(Ice.props);
// Send the request to the API server,
// and invoke onSearchRepsonse() with the response.
request.execute(onSearchResponse);
};
/* YouTube functions */
this.setChannelIdFromUsername = function(username) {
Ice.props.channelId = Ice.getChannelId(username);
};
this.getChannelId = function(username) {
var channelId = '';
function onSearchResponse(response) {
console.log(response);
Ice.props.channelId = response.items[0].id;
Ice.playVideos();
}
var request = window.gapi.client.youtube.channels.list({
part: 'snippet',
forUsername: username,
});
request.execute(onSearchResponse);
console.log({channelId: channelId });
return channelId;
};
}
this.returnChannelId = function() {
};
});