Angularjs 缓存与获取实时数据
Angularjs caching vs fetching live data
我有一项服务用于为应用程序获取数据。
由于服务是单例的,我想在服务中缓存一些参考数据,但我不确定如何实现。
我正在使用 Strongloop,现在在服务中有这样一个功能:
function fetchReferenceData() {
return refInfo.find().$promise;
}
我想要一个 属性 来存储参考数据。
我可以在代码中添加一个 属性 ,比如
var myRefData;
然后我可以编辑 fetchReferenceData() 来检查 属性,但是由于客户期望返回承诺,所以这行不通:
function fetchReferenceData() {
if (myRefData) { return myRefData; }
else { return refInfo.find().$promise;}
}
处理这种事情的好的编程模式是什么?
我是否传入一个函数来调用 .then() 以在客户端中设置数据?
如果客户端期望一个承诺,您可以使用 $q
服务从您的缓存值创建一个承诺。
function fetchReferenceData() {
if (myRefData) { return $q.when(myRefData); }
else { return refInfo.find().$promise;}
}
客户端可以使用标准.then
方法访问它。
fetchReferenceData().then (function (myRefData) {
//use myRefData
});
最佳实践
您需要一种方法来销毁缓存中的信息。
如果查询已经在进行中,您需要避免再次调用 refInfo.find()
。
ngResource 对象更新
方法来自 Angular $resource
services return 对空对象的引用。
来自文档:
It is important to realize that invoking a $resource object method immediately returns an empty reference (object or array depending on isArray
). Once the data is returned from the server the existing reference is populated with the actual data. This is a useful trick since usually the resource is assigned to a model which is then rendered by the view. Having an empty object results in no rendering, once the data arrives from the server then the object is populated with the data and the view automatically re-renders itself showing the new data. This means that in most cases one never has to write a callback function for the action methods.
因此您可以缓存查询引用,但您需要注意其上的数据将在一段时间内未定义。
$resource
服务还添加了一个 $promise
属性 可供接受承诺的客户端使用。
function fetchReferenceData() {
var cache;
return cache = cache || refInfo.find();
}
clientThatNeedsPromise(fetchReferenceData().$promise);
我有一项服务用于为应用程序获取数据。
由于服务是单例的,我想在服务中缓存一些参考数据,但我不确定如何实现。
我正在使用 Strongloop,现在在服务中有这样一个功能:
function fetchReferenceData() {
return refInfo.find().$promise;
}
我想要一个 属性 来存储参考数据。 我可以在代码中添加一个 属性 ,比如 var myRefData;
然后我可以编辑 fetchReferenceData() 来检查 属性,但是由于客户期望返回承诺,所以这行不通:
function fetchReferenceData() {
if (myRefData) { return myRefData; }
else { return refInfo.find().$promise;}
}
处理这种事情的好的编程模式是什么? 我是否传入一个函数来调用 .then() 以在客户端中设置数据?
如果客户端期望一个承诺,您可以使用 $q
服务从您的缓存值创建一个承诺。
function fetchReferenceData() {
if (myRefData) { return $q.when(myRefData); }
else { return refInfo.find().$promise;}
}
客户端可以使用标准.then
方法访问它。
fetchReferenceData().then (function (myRefData) {
//use myRefData
});
最佳实践
您需要一种方法来销毁缓存中的信息。
如果查询已经在进行中,您需要避免再次调用
refInfo.find()
。
ngResource 对象更新
方法来自 Angular $resource
services return 对空对象的引用。
来自文档:
It is important to realize that invoking a $resource object method immediately returns an empty reference (object or array depending on
isArray
). Once the data is returned from the server the existing reference is populated with the actual data. This is a useful trick since usually the resource is assigned to a model which is then rendered by the view. Having an empty object results in no rendering, once the data arrives from the server then the object is populated with the data and the view automatically re-renders itself showing the new data. This means that in most cases one never has to write a callback function for the action methods.
因此您可以缓存查询引用,但您需要注意其上的数据将在一段时间内未定义。
$resource
服务还添加了一个 $promise
属性 可供接受承诺的客户端使用。
function fetchReferenceData() {
var cache;
return cache = cache || refInfo.find();
}
clientThatNeedsPromise(fetchReferenceData().$promise);