我是 breeze.js 的新手,我在想出一种结合 executeQuery
和 executeQueryLocally
的好方法时遇到了一些麻烦。
用例是这样的:我想使用 Breeze 数据缓存来隐藏第 3 方网络服务的脆弱性。我想提出一种模式来查询服务并在调用时服务不可用时回退到缓存。
几天来我一直在思考这个问题 - 任何建议或建议都将不胜感激!
最佳答案
我认为这个解决方案可能是个好方法:
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)));
}
};
通过这个解决方案,我试图轻松地检查每个查询是否出了问题,而不是重复代码。
希望对您有所帮助。
关于breeze - 使用 executeQuery/executeQueryLocally 作为 Web 服务和缓存之间的中介,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16251578/