如何继续等待来自异步getGeolocation()函数的数据的Backbone model.save?

时间:2016-01-21 11:12:19

标签: javascript ajax asynchronous backbone.js

我有一个函数getLocation(),可以使用HTML5获取地理位置。

var environmentInfo = {
  OS: OSName,
  browser: browserName,
  browserVersion: fullVersion
};

function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(successCallback, showError);
  } else {
    environmentInfo.geoServiceNotSupported = true;
  }
}

function successCallback(position) {
  environmentInfo.geoServiceRefused = false;
  environmentInfo.latitude = position.coords.latitude;
  environmentInfo.longitude = position.coords.longitude;
}

function showError(error) {
  switch (error.code) {
    case error.PERMISSION_DENIED:
      environmentInfo.geoServiceRefused = true;
      break;
  }
}


this.activity = this.transaction.save({
        envData: {
          "Environment-GeoService-Refused-By-User": this.environmentInfo.geoServiceRefused,
          "Environment-GeoService-Not-Supported": this.environmentInfo.geoServiceNotSupported,
          "Meta-Geolocation-Lat": this.environmentInfo.latitude,
          "Meta-Geolocation-Long": this.environmentInfo.longitude
        }
      },
      {
        success: this.onStartTransaction,
        error: this.onStartTransactionError
      });

我还有一个Backbone.Model,它存储和保存一些数据,包括地理位置。 但我需要一个逻辑,它将等待模型中的地理定位,然后才能保存到后端。

1 个答案:

答案 0 :(得分:0)

this.activity = this.transaction.save()移入您的成功回调"对于地理定位。