From cf7c590aec01f754b380e723f65bb077fe800bec Mon Sep 17 00:00:00 2001 From: suVene Date: Fri, 26 Dec 2008 12:39:00 +0000 Subject: add wedata class. git-svn-id: http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk@27455 d0d07461-0603-4401-acd4-de1884942a52 --- _libly.js | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) (limited to '_libly.js') diff --git a/_libly.js b/_libly.js index 821b923..b699f03 100644 --- a/_libly.js +++ b/_libly.js @@ -12,7 +12,7 @@ var PLUGIN_INFO = 適当なライブラリっぽいものたち。 suVene MIT - 0.1.14 + 0.1.15 1.2 2.0pre http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk/_libly.js @@ -21,6 +21,7 @@ var PLUGIN_INFO = - liberator.plugins.libly.$U - liberator.plugins.libly.Request - liberator.plugins.libly.Response +- liberator.plugins.libly.Wedata == Logger == getLogger(prefix): @@ -503,6 +504,65 @@ libly.Response.prototype = { }; //}}} +libly.Wedata = function(dbname) { // {{{ + this.initialize.apply(this, arguments); +}; +libly.Wedata.prototype = { + initialize: function(dbname) { + this.HOST_NAME = 'http://wedata.net/'; + this.dbname = dbname; + this.logger = libly.$U.getLogger('libly.Wedata'); + }, + getItems: function(expire, itemCallback, finalCallback) { + + var logger = this.logger; + var STORE_KEY = 'plugins-libly-wedata-items'; + var store = storage.newMap(STORE_KEY, true); + + expire = expire || 0; + + if (store && store.get('data') && new Date(store.get('expire')) > new Date()) { + logger.log('return cache. '); + store.get('data').forEach(function(item) itemCallback(item)); + finalCallback(true, store.get('data')); + return; + } + + function errDispatcher(msg, cache) { + if (cache) { + logger.log('return cache. -> ' + msg); + cache.forEach(function(item) itemCallback(item)); + finalCallback(true, cache); + } else { + finalCallback(false, msg); + } + } + + var req = new libly.Request(this.HOST_NAME + 'databases/' + this.dbname + '/items.json'); + req.addEventListener('onSuccess', libly.$U.bind(this, function(res) { + var text = res.responseText; + if (!text) { + errDispatcher('respons is null.', store.get('data')); + return; + }; + var json = libly.$U.evalJson(text); + if (!json) { + errDispatcher('uailed eval json.', store.get('data')); + return; + }; + store.set('expire', new Date(new Date().getTime() + expire).toString()); + store.set('data', json); + store.save(); + json.forEach(function(item) itemCallback(item)); + finalCallback(true, json); + })); + req.addEventListener('onFailure', function() errDispatcher('onFailure')); + req.addEventListener('onException', function() errDispatcher('onException') ); + req.get(); + } +}; +//}}} + //} // vim: set fdm=marker sw=4 ts=4 sts=0 et: -- cgit v1.2.3