我想我的對象緩存一些網絡請求的結果,並回答緩存的值,而不是做一個新的請求。 This answer here完成使用角度承諾看起來很像我要去的,但我不知道如何使用Parse.com諾言庫來表達它。這是我想要...緩存結果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;
});
},
我的想法是返回一個承諾,無論該值是否緩存。在緩存該值的情況下,該承諾立即解決。問題是,正如我在調試器中所看到的,我似乎沒有在第二次調用時獲得緩存結果。
你爲什麼不只是緩存當初的諾言,並返回? – JoseM
我也沒有看到Parse.Object通過本機JavaScript在這裏提供了什麼。 –
@BenjaminGruenbaum我不完全確定,但他們的示例代碼是這樣做的。至少有一件事情是實現諸如save()和destroy()之類的東西。 – someShmuck