X-Git-Url: http://git.bytex64.net/?a=blobdiff_plain;f=www%2Fjssrc%2Fblerg%2FAPI.js;h=986ac0963cfe01089017b6671671bc8f6dada3e0;hb=0085ebfed447d3e7a828eb691a78c5ccc2c21a8c;hp=203706a674da60477844ccca4312302ecb89885d;hpb=04a4fdf9d85e0d0b193300abe0c0e433449cd285;p=blerg.git diff --git a/www/jssrc/blerg/API.js b/www/jssrc/blerg/API.js index 203706a..986ac09 100644 --- a/www/jssrc/blerg/API.js +++ b/www/jssrc/blerg/API.js @@ -32,12 +32,37 @@ enyo.kind({ blerg.API.apiInitialized = true; }, + signup: function(username, password) { + var req = new enyo.Ajax({ + url: baseURL + '/create', + method: 'POST', + postBody: { + username: username, + password: password, + } + }); + req.response(this, function(inSender, inResponse) { + if (inResponse.status == 'success') { + this.bubble('onSignupSuccess', {username: username}); + } else { + this.bubble('onSignupFailure', {username: username}); + } + }); + req.error(this, function() { + this.bubble('onSignupFailure', {username: username}); + }); + req.go(); + }, login: function(username, password) { var req = new enyo.Ajax({ url: baseURL + '/login', - method: 'POST' + method: 'POST', + postBody: { + username: username, + password: password + } }); - req.response(function(inSender, inResponse) { + req.response(this, function(inSender, inResponse) { if (inResponse.status == 'success') { blerg.API.loggedIn = true; blerg.API.username = username; @@ -47,27 +72,51 @@ enyo.kind({ enyo.setCookie('username', '', {"Max-Age": 0}); this.bubble('onLoginFailed'); } - }.bind(this)); - req.go({ - username: username, - password: password }); + req.error(this, function() { + enyo.setCookie('username', '', {"Max-Age": 0}); + this.bubble('onLoginFailed'); + }); + req.go(); }, logout: function() { var req = new enyo.Ajax({ url: baseURL + '/logout', - method: 'POST' + method: 'POST', + postBody: { + username: blerg.API.username + } }); - req.response(function(inSender, inResponse) { + req.response(this, function(inSender, inResponse) { blerg.API.loggedIn = false; enyo.setCookie('auth', '', {"Max-Age": 0}); this.bubble('onLogoutSuccessful'); - }.bind(this)); - req.go({ - username: blerg.API.username }); + req.go(); enyo.setCookie('username', '', {"Max-Age": 0}); }, + changePassword: function(oldpassword, newpassword) { + var req = new enyo.Ajax({ + url: baseURL + '/passwd', + method: 'POST', + postBody: { + username: blerg.API.username, + password: oldpassword, + new_password: newpassword + } + }); + req.response(this, function(inSender, inResponse) { + if (inResponse.status == 'success') { + this.bubble('onPasswordChangeSuccessful'); + } else { + this.bubble('onPasswordChangeFailed'); + } + }); + req.error(this, function() { + this.bubble('onPasswordChangeFailed'); + }); + req.go(); + }, loadUserRecords: function(username, from ,to) { var url; if (from != undefined && to != undefined) { @@ -88,6 +137,12 @@ enyo.kind({ entries: inResponse }); }.bind(this)); + req.error(function(inSender, inResponse) { + if (inResponse == 404) + this.bubble('onUserNotFound'); + else + this.bubble('onAPIError', {response: inResponse}); + }.bind(this)); req.go(); }, loadTagRecords: function(type, tag) { @@ -108,14 +163,22 @@ enyo.kind({ var req = new enyo.Ajax({ url: url }); - req.response(function(inSender, inResponse) { + req.response(this, function(inSender, inResponse) { this.bubble('onItemsLoaded', { type: 'tag', tagType: type, tag: tag, entries: inResponse }); - }.bind(this)); + }); + req.error(this, function() { + this.bubble('onItemsLoaded', { + type: 'tag', + tagType: type, + tag: tag, + entries: [] + }); + }); req.go(); }, getFeedInfo: function() { @@ -124,14 +187,15 @@ enyo.kind({ var req = new enyo.Ajax({ url: baseURL + '/feedinfo', - method: 'POST' + method: 'POST', + postBody: { + username: blerg.API.username + } }); - req.response(function(inSender, inResponse) { + req.response(this, function(inSender, inResponse) { this.bubble('onFeedInfo', inResponse); - }.bind(this)); - req.go({ - username: blerg.API.username }); + req.go(); }, loadFeed: function() { if (!blerg.API.loggedIn) @@ -139,61 +203,89 @@ enyo.kind({ var req = new enyo.Ajax({ url: baseURL + '/feed', - method: 'POST' + method: 'POST', + postBody: { + username: blerg.API.username + } }); - req.response(function(inSender, inResponse) { + req.response(this, function(inSender, inResponse) { this.bubble('onItemsLoaded', { type: "feed", entries: inResponse }); - }.bind(this)); - req.go({ - username: blerg.API.username }); + req.go(); }, getSubscriptionStatus: function(username) { var req = new enyo.Ajax({ url: baseURL + '/feedinfo/' + username, - method: 'POST' + method: 'POST', + postBody: { + username: blerg.API.username + } }); - req.response(function(inSender, inResponse) { + req.response(this, function(inSender, inResponse) { this.bubble('onSubscriptionStatus', { username: username, subscribed: inResponse.subscribed }); - }.bind(this)); - req.go({ - username: blerg.API.username }); + req.go(); }, subscribe: function(username) { var req = new enyo.Ajax({ url: baseURL + '/subscribe/' + username, - method: 'POST' + method: 'POST', + postBody: { + username: blerg.API.username + } }); - req.response(function(inSender, inResponse) { + req.response(this, function(inSender, inResponse) { this.bubble('onSubscriptionStatus', { username: username, subscribed: inResponse.status == "success" }); - }.bind(this)); - req.go({ - username: blerg.API.username }); + req.go(); }, unsubscribe: function(username) { var req = new enyo.Ajax({ url: baseURL + '/unsubscribe/' + username, - method: 'POST' + method: 'POST', + postBody: { + username: blerg.API.username + } }); - req.response(function(inSender, inResponse) { + req.response(this, function(inSender, inResponse) { this.bubble('onSubscriptionStatus', { username: username, subscribed: inResponse.status != "success" }); - }.bind(this)); - req.go({ - username: blerg.API.username }); + req.go(); }, + post: function(data) { + var req = new enyo.Ajax({ + url: baseURL + '/put', + method: 'POST', + postBody: { + username: blerg.API.username, + data: data + } + }); + req.response(this, function(inSender, inResponse) { + if (inResponse && inResponse.status == 'success') { + this.bubble('onPostSuccessful', { + username: blerg.API.username, + data: data + }); + } else { + this.bubble('onPostFailed', { + username: blerg.API.username, + data: data + }); + } + }); + req.go(); + } });