私はYouTubeAPIを使用して、動画のリストの情報を取得しています。したがって、Request.JSONPクラスを使用する必要があります。
私の問題は、YouTube APIを複数回呼び出す必要があり、それを「チェーン」にできないことです。ドキュメントによると、必要なのはオプションを追加し、link:'chain'
を使用して同期モードで実行することだけですasync:false
(witchは単純なRequestオブジェクトで正常に機能します)。
これは私が試したことです:
var MyClass = new Class({
listVideos: [],
videosInfo: [],
initialize : function(listVideos){
this.listVideos = listVideos;
this.getVideosInfo();
},
getVideosInfo : function(){
var request = new Request.JSONP({
callbackKey: 'callback',
link : 'chain',
async : false,
onComplete: function(data){
this.videosInfo.push(data.entry);
console.log(this.videosInfo);
}.bind(this)
});
this.listVideos.each(function(videoId){
request.options.url = 'http://gdata.youtube.com/feeds/api/videos/' + videoId + '?v=2&alt=json-in-script&format=5';
request.send();
});
console.log("this should be displayed AFTER the requests, not BEFORE u_u");
console.log(this.videosInfo);
}
});
var test = new MyClass(['AJso1SJT7Js','hlO5UBxuJlY'])
これがjsfiddleなので、 n_nで遊ぶことができます
基本的に私が欲しいのは、videosInfo
すべてのリクエストが完了した後に配列を処理することです。
何か案は?
前もって感謝します
PS: 私は自分の問題に対する「汚い解決策」を見つけました、http://jsfiddle.net/pleasedontbelong/gLF5h/1/ しかし、リクエストの1つが失敗した場合、私はそれをまったく快適に感じません。不合格