2016-10-05 1 views
0

APIサーバー(この場合はform.io)に保存されているアプリケーション用の設定データがありますが、ほとんど変更されないためlocalStorageに保存します必要に応じて更新してください。だから私のgetStationConfigサービスでは、私はlocalStorageのデータをチェックし、そこになければ、APIから取得し、次回のためにローカルに保存します。ローカルに保存されたデータのサービスから約束を返す

約束事を期待している私のコントローラコードがそれを処理できるように、問題は約束事でlocalStorageからデータを戻しています。私のコントローラからデータを取得するためのコードはこれです:

var stationPromise = shiftService.getStationConfig().then(function(data) { 
    vm.configSlots = data; 
}); 

と私のサービスは、次のようになります、のlocalStorage(設定VAR時から返されたデータを書き込まれているとおり

var getStationConfig = function() { 
    // First check to see if info is stored locally. 
    var config = JSON.parse(localStorage.getItem('cfdConfig')); 
    if (config) { 
    return config; 
    } 

    // Get position info from appropriate Config resource submission. 
    // Code borrowed from ngFormioHelper.FormioAuth. 
    var token = localStorage.getItem('formioToken'); 
    var config = { 
    disableJWT: true, 
    headers: { 
     "x-jwt-token": token 
    } 
    }; 
    return $http.get(Formio.getAppUrl() + '/config/submission', config) 
    .then(function(result) { 
     // Code goes here to format the data for return to the controller 

     // Now that it's all formatted, store the info in localstorage so that it can be retrieved from there 
     // later and we don't have to keep hitting the API for it. 
     localStorage.setItem('cfdConfig', JSON.stringify(allSlots)); 

     return allSlots; 
    }, 
    function(err) { 
     console.log(err); 
    }); 
}; 

上部)は、下部のallSlotsと同じフォーマットですが、それは約束ではないので、コントローラはそれを気に入らないのです。それを約束どおりに包むか返すことができますか? $ http.get()経由でlocalStorageを呼び出す必要がありますか(可能なら)?それとも別の方法ですか?

ありがとうございました。

答えて

2

注入するあなたのサービスに$q約束サービスと約束

if (config) { 
    return $q.resolve(config); 
} 
+0

パーフェクトに包まれたデータを返すためにそれを使用。私はすでに他の場所で$ qを使っていましたが、ここでそれについて考えることはありませんでした。どうもありがとう。 – wonder95

関連する問題