开发者

Cache won't work in Appcelerator

Titanium SDK version: 1.6.

iPhone SDK version: 4.2

I am trying out the ca开发者_运维技巧che snippet found on the Appcelerator forum but I get an error: [ERROR] Script Error = Can't find variable: utils at cache.js (line 9).

I put this one (http://pastie.org/1541768) in a file called cache.js and implemented the code from this one (http://pastie.org/pastes/1541787) in the calling script, but I get the error.

What is wrong? I copied the code exactly.


Your problems is whilst the first pastie defines utils.httpcache. The variable utils is not defined outside of this function closure (because it is not defined anywhere in global namespace). As below shows.

(function() {
  utils.httpcache = {

  };
})();

To make it all work in this instance add the following code to the top of your cache.js file.

var utils = {};

This declares the utils variable in global namespace. Then when the function closure is executed below it will add utils.httpcache to the utils object.

The problem is actually not specific to Appcelerator and is just a simple JavaScript bug. Checkout Douglas Crockfords book, JavaScript the Good Parts. Reading it will literally make you a more awesome JavaScript developer.


You can't use utils.httpcache.getFromCache(url) until you add this to your code:

var utils = {};

That's because how the author created his function, it's called JavaScript module pattern and it's generally used to structure the code.


I seem to lose this value "value.httpCacheExpire = expireTime;" when the code does the "Titanium.App.Properties.setString(key,JSON.stringify(value));" so when I get it back using the getString method, there's no longer the "value.httpCacheExpire.

Anyone else have this issue? Am I missing something to get this working?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜