【发布时间】:2014-05-19 20:40:51
【问题描述】:
我希望我的对象缓存某些网络请求的结果并回答缓存的值,而不是执行新请求。 This answer here 使用角度承诺完成看起来很像我想要的,但我不确定如何使用 Parse.com 承诺库来表达它。这就是我正在尝试的...
module.factory('CustomObject', function() {
var CustomObject = Parse.Object.extend("CustomObject", {
cachedValue: null,
getValue: function() {
if (this.cachedValue) return Parse.Promise.as(this.cachedValue);
return this.functionReturningPromise().then(function (theValue) {
this.cachedValue = theValue;
return this.cachedValue;
});
},
我的想法是返回一个承诺,无论该值是否被缓存。在缓存值的情况下,该承诺会立即解决。问题是,当我在调试器中执行此操作时,我似乎没有在第二次调用时获得缓存结果。
【问题讨论】:
-
你为什么不缓存原始的承诺并返回呢?
-
我也看不到 Parse.Object 在本机 JavaScript 上为您提供了什么。
-
@BenjaminGruenbaum 我不完全确定,但他们的示例代码就是这样做的。它至少做的一件事是实现了 save() 和 destroy() 之类的东西。
标签: angularjs parse-platform promise