2012-04-12 7 views
0

Dojoを使用して2つのxhr.getsを1つずつ順番に実行するにはどうすればよいですか?複数のxhr.getとdojo

私が持っている....

require(["dojo/_base/xhr", "dojo/dom", "dojo/domReady!"], 
function(xhr, dom) { 

    // Using xhr.get, as very little information is being sent 
    xhr.get({ 
     // The URL of the request 
     url: "inc/etl2json.php?item=Execs", 
     // The success callback with result from server 
     load: function(execContent) { 
      dom.byId("Execs").innerHTML = execContent; 
     }, 
     // The error handler 
     error: function() { 
      // Do nothing -- keep old content there 
     } 
    }); 

}); 

私は"INC/etl2json.php?項目=幹部"に別のxhr.getを行うと、「(dom.byIdに割り当てたいですElapsed ")。innerHTML = elapsedContent;

答えて

1

呼び出すだけもう一度だけでなく内容が変更になっている場合は、他のあなただけの同じデータを使用することができることを初めて取得し、負荷の関数内()xhr.get:私は思う

xhr.get({ 
    load:function(data){ 
     //use the first data you retrieved 
     xhr.get({ 
      load: function(data2){ 
      //do what you like with the nuew data 
      } 
     }); 
    } 
}); 
0

elapsedContentの別のxhrコールを追加する必要があります。私は2つの呼び出しの間に関係がないので、それらを別々にする必要があります。 1つを別のものに入れ子にする必要はありません。 だけネスティングが、それはほとんど常に読めないコードにつながる簡単な解決策ですが

xhr.get({ 
     // The URL of the request 
     url: "inc/etl2json.php?item=Execs", 
     // The success callback with result from server 
     load: function(elapsedContent) { 
      dom.byId("Elapsed").innerHTML = elapsedContent; 
     }, 
     // The error handler 
     error: function() { 
      // Do nothing -- keep old content there 
     } 
    }); 
1

を追加するので、私は@Ricardoが行ったように同じことを行うだろうが、DojoのDeferred(+ here)の利点を使用して、連鎖採用:

var requestUrl = "inc/etl2json.php?item=Execs"; 

xhr.get({ url: requestUrl}) 
    .then(function(results) { 
     dom.byId("execs").innerHTML = results; 
    }) 
    .then(function(results) { 
     return xhr.get({ url: requestUrl}); 
    }) 
    .then(function(results) { 
     dom.byId("elapsed").innerHTML = results; 
    }) 

jsFiddleでの行動でそれを参照してください:http://jsfiddle.net/phusick/73X88/

関連する問題