I'm new to breeze.js and I'm having a little trouble coming up with a good way to combine executeQuery and executeQueryLocally.
The use case is this: I want to use breeze data caching to hide the flakiness of a 3rd party web service. I'd like to come up with a pattern that queries the service and falls back to the cache if the service is unavailable when called.
I've been chewing on this for a couple of days now - any suggestions or advice would be appreciated!
I think that this solution can be a good way:
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)));
}
};
With this solution I have tried to to do easy to check in every query if it is something that is going wrong and not to repeat code.
I hope this can help you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With