3
我是breeze.js的新手,並且遇到了一個將executeQuery
和合並的好方法。使用executeQuery/executeQueryLocally作爲Web服務和緩存之間的中介
用例如下:我想使用微風數據緩存來隱藏第三方Web服務的片狀。我想提出一種查詢服務的模式,如果服務在被調用時不可用,則退回到緩存。
我一直咀嚼這一兩天了 - 任何建議或意見將不勝感激!
我是breeze.js的新手,並且遇到了一個將executeQuery
和合並的好方法。使用executeQuery/executeQueryLocally作爲Web服務和緩存之間的中介
用例如下:我想使用微風數據緩存來隱藏第三方Web服務的片狀。我想提出一種查詢服務的模式,如果服務在被調用時不可用,則退回到緩存。
我一直咀嚼這一兩天了 - 任何建議或意見將不勝感激!
我認爲,這個解決方案可以是一個好辦法:
executeQuery= function(query){
operating(true);
return manager.executeQuery(query).fail(fail);
function fail(error){
//You can decide if you want to query locally depending on the type of error
//Example: if(error.status===404) ;
return executeQueryLocally(query);
}
}
executeQueryLocally= function(query){
return manager.executeQuery(query).using(FetchStrategy.FromLocalCache).fail(fail);
function fail(error){
//You can't get the information, so you can throw an error
//Or that you want
throw Error('Impossible to get the requested data');
}
}
//Example calling this methods
var getCustomers= function(resultArrayObservable,inlineCountObservable){
var query = new breeze.EntityQuery("Customers").inlineCount(true);
return executeQuery(query).then(success);
function success(data){
inlineCountObservable(data.inlineCount);
resultArrayObservable(standarizeCustomerDtos(mapCustomerDtosToKos(data.results)));
}
};
有了這個解決方案,我試圖做簡單的在每一個查詢,以檢查它是否是走錯了,而不是重複代碼的東西。
我希望這可以幫助你。