0

次のコード スニペットに問題があります。私の init メソッドは、initializeMapp() と geoListen() を実行する前に、getLocation() 関数を実行して完了する必要があります。リソースとしてリンクされた rsvp.js がありますが、実装方法がよくわかりません。jQuery の $when.done メソッドも試しました。どんな助けでも大歓迎です。

jQuery メソッド:

//initial page load method
function init() {

    //get user location then builds map and listens to database
    $.when(getLocation()).done.(function () {

       //build map now that you have user location
        initializeMap();

       //Starts listening to database changes
       geoListen();

    })

}

出欠確認方法:

 //initial page load method
 function init() {

      //get user location then builds map and listens to database
     getLocation().then(function () {

     //build map now that you have user location
     initializeMap();

     //Starts listening to database changes
     geoListen();

  })

 }   
4

1 に答える 1

0

getLocation関数にコールバックを受け入れさせ、getLocation終了時にそれらを実行させることができます

getLocation = function(cb1, cb2) {
  // when done 
  cb1()
  cb2()
}

function init() {
  getLocation(initializeMap, geoListen)
}

または、コールバック参照の配列を渡し、それらをループしてgetLocation呼び出すことができます

または、Q.js を使用して promise を返すこともできます。getLocation

于 2014-09-12T21:04:50.967 に答える