Last active
February 23, 2016 17:28
-
-
Save renatorib/49185d6458effc5245a7 to your computer and use it in GitHub Desktop.
api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var Api = { | |
| request: function(method, url, data, hooks){ | |
| hooks = hooks || {}; | |
| fail = hooks.fail || function(){}; | |
| done = hooks.done || function(){}; | |
| always = hooks.always || function(){}; | |
| before = hooks.before || function(){}; | |
| spinner = hooks.spinner || false; | |
| reloadOnSuccess = hooks.reloadOnSuccess || false; | |
| ajaxSpinner = hooks.ajaxSpinner || true; | |
| mouseSpinner = hooks.mouseSpinner || true; | |
| alertErrors = hooks.showErrors || true; | |
| $.ajax({ | |
| method: method, | |
| url: url, | |
| data: data, | |
| dataType: 'json', | |
| beforeSend: function(xhr){ | |
| if(ajaxSpinner) Ui.ajaxSpinnerShow(); | |
| if(mouseSpinner) Ui.mouseSpinnerShow(); | |
| if(spinner) Ui.spinnerShow(); | |
| if($.isFunction(before)) before(xhr); | |
| } | |
| }) | |
| .done(function(res, status) { | |
| ga('send', 'event', 'ajax', 'done', method+' '+url); | |
| res = res || {}; | |
| if(res.redirect) window.location = res.redirect + '?redirect=' + window.location; | |
| if(res.success && reloadOnSuccess) location.reload(); | |
| if(!res.success && res.error && alertErrors) Ui.alert(res.error); | |
| if($.isFunction(done)) done(res, status); | |
| if(res.validationErrors){ | |
| $.each(res.validationErrors, function(field, error){ | |
| $("[name='"+field+"']").addClass('error'); | |
| $("[data-feedback-msg='"+field+"']").text(error).fadeIn(); | |
| $("[data-feedback-class='"+field+"']").addClass('has-error'); | |
| }); | |
| } | |
| }) | |
| .fail(function(xhr, status){ | |
| ga('send', 'event', 'ajax', 'fail', method+' '+url); | |
| if(alertErrors) Ui.alert('Connection error: ' + status); | |
| if($.isFunction(fail)) fail(xhr, status); | |
| }) | |
| .always(function(res, status){ | |
| if(ajaxSpinner) Ui.ajaxSpinnerHide(); | |
| if(mouseSpinner) Ui.mouseSpinnerHide(); | |
| if(spinner) Ui.spinnerHide(); | |
| if($.isFunction(always)) always(res, status); | |
| }); | |
| }, | |
| Admin: { | |
| users: { | |
| put: function(data, cbs){ | |
| Api.request('POST', '/api/admin/edituser', {data: data}, cbs); | |
| } | |
| } | |
| }, | |
| Account: { | |
| profileImg: { | |
| post: function(img, cbs){ | |
| Api.request('POST', '/api/accounts/profile_img', {img: img}, cbs); | |
| } | |
| }, | |
| coverImg: { | |
| post: function(img, cbs){ | |
| Api.request('POST', '/api/accounts/cover_img', {img: img}, cbs); | |
| } | |
| }, | |
| readAllNotifications: { | |
| post: function(cbs){ | |
| Api.request('POST', '/api/accounts/read_all_notifications', {}, cbs); | |
| } | |
| } | |
| }, | |
| Profile: { | |
| Follow: { | |
| post: function(id, cbs){ | |
| Api.request('POST', '/api/profiles/follow', {target_id: id}, cbs); | |
| } | |
| } | |
| }, | |
| Photo: { | |
| post: function(data, cbs){ | |
| Api.request('POST', '/api/photos/upload', data, cbs); | |
| }, | |
| delete: function(id, cbs){ | |
| Api.request('DELETE', '/api/photos/delete', {photo_id: id}, cbs); | |
| }, | |
| put: function(data, cbs){ | |
| Api.request('PUT', '/api/photos/edit', data, cbs); | |
| }, | |
| ToggleComment: { | |
| post: function(id, cbs){ | |
| Api.request('POST', '/api/photos/toggle_comments', {photo_id: id}, cbs); | |
| } | |
| }, | |
| Like: { | |
| post: function(id, cbs){ | |
| Api.request('POST', '/api/photos/like', {photo_id: id}, cbs); | |
| } | |
| }, | |
| Likes: { | |
| get: function(id, cbs){ | |
| Api.request('GET', '/api/photos/likes', {photo_id: id}, cbs); | |
| } | |
| }, | |
| Comment: { | |
| post: function(id, comment, cbs){ | |
| Api.request('POST', '/api/photos/comment', {photo_id: id, comment: comment}, cbs); | |
| }, | |
| delete: function(id, cbs){ | |
| Api.request('DELETE', '/api/photos/comment', {comment_id: id}, cbs); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment