(function(){ // qora() makes a deferred api call window.qora = function(path, param) { if (param == null) param = {type:'get', apiurl:path}; else param = {type:'post', apiurl:path, json:JSON.stringify(param)}; return $.post('/index/api.html', param).then(function(result){ switch(result.type) { case 'success': return JSON.parse(result.result); case 'apicallerror': return (new $.Deferred()).reject(new qora.APIError(result.errordetail)); case 'error': return (new $.Deferred()).reject(new qora.WebError(result.error)); } }).promise(); }; // provided to fail handlers qora.APIError = function(json) { json = JSON.parse(json); this.error = json.error; this.message = json.message; }; qora.WebError = function(err) { try { err = JSON.parse(error); } catch(e) {} this.message = err; }; // automatically extracts value, fails if not found qora.namestorage = function(name, key) { return qora('namestorage/' + name + '/key/' + key).then(function(result) { if (result[key]) return result[key]; else return (new $.Deferred()).reject(new qora.APIError('{"message":"the key does not exist","error":119}')); }).promise(); }; })();