2011-12-21 10 views
0

私は10個のオブジェクトのセットを持っていて、それらのオブジェクトの1つに、Javascript経由で呼び出されたAPIから取得した追加情報が必要であるとします。私の場合、このオブジェクトはSoundcloudの埋め込みであり、私はそのAPIから追加情報が必要です。同期コンテナ内の非同期Javascriptコールのベストプラクティス?

jQueryでは$.getJSONを使用し、解決された値をグローバル配列にプッシュできますが、スプライシングコードを大量に使用することなく正しい順序で配置されているとは限りません。

私のリストを順番に保つためのベストプラクティスは何ですか?

例:

var embeds = [{type: "mp3", url: "http://example.com/song.mp3", artist: "Artist"}, 
       {type: "mp3", url: "http://anotherexample.com/song.mp3", artist: "Artist 2"}, 
       {type: "soundcloud", url: "http://soundcloud.com/summergirlfriends/shockwaves"}]; 
for(e in embeds) { 
    if(embeds[e].type == "soundcloud") { 
     // here is where i need to call the SC API and do something with the results 
    } 
} 
+2

「何正しい順序で」という意味ですか?私たちは何を「リスト」していますか?あなたがコードを投稿した場合、それは多くの助けになります。 – Pointy

+0

何を試しましたか?そして、コードを見ることはできますか? – Jasper

+0

私は、小文字の例を追加しました – Carson

答えて

4

私が正しくあなたを理解している場合、私は何を探していることは、あなたが正しい埋め込みを修正していることを確認するために閉鎖されると思う:

var embeds = [{type: "mp3", url: "http://example.com/song.mp3", artist: "Artist"}, 
       {type: "mp3", url: "http://anotherexample.com/song.mp3", artist: "Artist 2"}, 
       {type: "soundcloud", url: "http://soundcloud.com/summergirlfriends/shockwaves"}]; 
for(var i=0;i<embeds.length;i++) { 
    if(embeds[i].type == "soundcloud") { 
     (function(embed){ 
      $.ajax({ 
       //... 
       success:function(newData){ 
        embed['additionalData']=newData; 
        console.log(embeds); 
       } 
      }); 
     })(embeds[i]) 
    } 
} 
+0

神が祝福を祝福しました。ありがとう! – Carson

関連する問題