X-Git-Url: http://git.bytex64.net/?a=blobdiff_plain;f=www%2Fjssrc%2Fblerg%2FAPI.js;fp=www%2Fjssrc%2Fblerg%2FAPI.js;h=1a6b0133816537f82bb6d9eaa3000650f7bd333f;hb=15917511e7ec895e1caf6464a7deef76ed448f76;hp=2fb9561464620d9f1ff7a36a0ecfe47c8421df3a;hpb=65a1f7dc82554486ffa9cc03eac29a7a1642f046;p=blerg.git diff --git a/www/jssrc/blerg/API.js b/www/jssrc/blerg/API.js index 2fb9561..1a6b013 100644 --- a/www/jssrc/blerg/API.js +++ b/www/jssrc/blerg/API.js @@ -1,19 +1,36 @@ var baseURL = ''; +// The API state is static so that any instance can use login-dependent API +// calls enyo.kind({ name: "blerg.API", - loggedIn: false, - username: "", + kind: "Component", + statics: { + apiInitialized: false, + loggedIn: false, + username: "", + }, create: function() { this.inherited(arguments); + if (blerg.API.apiInitialized) { + if (blerg.API.loggedIn) { + setTimeout(function() { + this.bubble('onLoginSuccessful', {username: blerg.API.username}); + }.bind(this), 0); + } + return; + } + if (enyo.getCookie('auth') && enyo.getCookie('username')) { - this.loggedIn = true; - this.username = enyo.getCookie('username'); + blerg.API.loggedIn = true; + blerg.API.username = enyo.getCookie('username'); // Defer the signal until everything's initialized setTimeout(function() { - this.bubble('onLoginSuccessful', {username: this.username}); + this.bubble('onLoginSuccessful', {username: blerg.API.username}); }.bind(this), 0); } + + blerg.API.apiInitialized = true; }, login: function(username, password) { var req = new enyo.Ajax({ @@ -22,8 +39,8 @@ enyo.kind({ }); req.response(function(inSender, inResponse) { if (inResponse.status == 'success') { - this.loggedIn = true; - this.username = username; + blerg.API.loggedIn = true; + blerg.API.username = username; enyo.setCookie('username', username); this.bubble('onLoginSuccessful', {username: username}); } else { @@ -42,18 +59,36 @@ enyo.kind({ method: 'POST' }); req.response(function(inSender, inResponse) { - this.loggedIn = false; + blerg.API.loggedIn = false; enyo.setCookie('auth', '', {"Max-Age": 0}); this.bubble('onLogoutSuccessful'); }.bind(this)); req.go({ - username: this.username + username: blerg.API.username }); enyo.setCookie('username', '', {"Max-Age": 0}); }, requestFeedStatus: function() { - if (!this.loggedIn) + if (!blerg.API.loggedIn) throw new Error('Cannot request feed status when not logged in'); // TODO + }, + loadUserRecords: function(username, from ,to) { + var url; + if (from != undefined && to != undefined) { + url = baseURL + '/get/' + username + '/' + from + '-' + to; + } else { + url = baseURL + '/get/' + username; + } + + var req = new enyo.Ajax({ + url: url + }); + req.response(function(inSender, inResponse) { + this.bubble('onItemsLoaded', { + entries: inResponse + }); + }.bind(this)); + req.go(); } });