From b12ff0eb2058fe7c33471087aa9e1f2aa873f9ee Mon Sep 17 00:00:00 2001 From: Matthew Dillon Date: Thu, 12 Nov 2015 13:32:41 -0700 Subject: [PATCH] ember-ajax Fixes #37 Fixes #38 --- app/pods/protected/compare/results/route.js | 9 ++------ .../users/changepassword/controller.js | 13 +++-------- app/pods/users/new/verify/route.js | 9 +++----- .../users/requestlockouthelp/controller.js | 9 ++------ app/services/ajax.js | 23 +++++++++++++++++++ app/utils/ajax-request.js | 20 ---------------- package.json | 2 +- 7 files changed, 34 insertions(+), 51 deletions(-) create mode 100644 app/services/ajax.js delete mode 100644 app/utils/ajax-request.js diff --git a/app/pods/protected/compare/results/route.js b/app/pods/protected/compare/results/route.js index 5a8ec60..f71de62 100644 --- a/app/pods/protected/compare/results/route.js +++ b/app/pods/protected/compare/results/route.js @@ -1,10 +1,10 @@ import Ember from 'ember'; -import ajaxRequest from '../../../../utils/ajax-request'; const { Route, $: { isEmptyObject }, inject: { service } } = Ember; export default Route.extend({ session: service(), + ajax: service(), queryParams: { strain_ids: { @@ -33,12 +33,7 @@ export default Route.extend({ compare.set('selectedStrains', params.strain_ids); compare.set('selectedCharacteristics', params.characteristic_ids); - const url = `${this.get('globals.apiURL')}/api/${this.get('globals.genus')}/compare`; - const options = { - method: 'GET', - data: params, - }; - return ajaxRequest(url, options, this.get('session')); + return this.get('ajax').request('/compare', { data: params }) }, setupController: function(controller, model) { diff --git a/app/pods/protected/users/changepassword/controller.js b/app/pods/protected/users/changepassword/controller.js index 3aed813..9119f1a 100644 --- a/app/pods/protected/users/changepassword/controller.js +++ b/app/pods/protected/users/changepassword/controller.js @@ -1,24 +1,17 @@ import Ember from 'ember'; -import ajaxRequest from '../../../../utils/ajax-request'; const { Controller, inject: { service } } = Ember; export default Controller.extend({ session: service(), + ajax: service(), currentUser: service('session-account'), actions: { save: function(password) { - const url = `${this.get('globals.apiURL')}/api/${this.get('globals.genus')}/users/password`; const id = this.get('currentUser.account.id'); - const options = { - method: 'POST', - data: { - id: id, - password: password, - }, - }; - ajaxRequest(url, options, this.get('session')); + const data = { id: id, password: password }; + this.get('ajax').post('/users/password', { data: data }); this.transitionToRoute('protected.users.show', id); this.get('flashMessages').information('Your password has been changed.'); }, diff --git a/app/pods/users/new/verify/route.js b/app/pods/users/new/verify/route.js index 06755b3..a2d26c2 100644 --- a/app/pods/users/new/verify/route.js +++ b/app/pods/users/new/verify/route.js @@ -1,20 +1,17 @@ import Ember from 'ember'; -import ajaxRequest from '../../../../utils/ajax-request'; const { Route, inject: { service } } = Ember; export default Route.extend({ session: service(), - globals: service(), + ajax: service(), model: function(params) { - const url = `${this.get('globals.apiURL')}/api/${this.get('globals.genus')}/users/verify/${params.nonce}`; - return ajaxRequest(url, {}, this.get('session')); + return this.get('ajax').request(`/users/verify/${params.nonce}`); }, - afterModel: function(model/*, transition*/) { - this.get('flashMessages').success(model.get('msg')); + this.get('flashMessages').success(model.msg); this.transitionTo('login'); }, diff --git a/app/pods/users/requestlockouthelp/controller.js b/app/pods/users/requestlockouthelp/controller.js index 2930b31..321045d 100644 --- a/app/pods/users/requestlockouthelp/controller.js +++ b/app/pods/users/requestlockouthelp/controller.js @@ -1,20 +1,15 @@ import Ember from 'ember'; -import ajaxRequest from '../../../utils/ajax-request'; const { Controller, inject: { service } } = Ember; export default Controller.extend({ session: service(), globals: service(), + ajax: service(), actions: { submit: function(email) { - const url = `${this.get('globals.apiURL')}/api/${this.get('globals.genus')}/users/lockout`; - const options = { - method: 'POST', - data: { email: email }, - }; - ajaxRequest(url, options, this.get('session')); + this.get('ajax').post('/users/lockout', { data: { email: email } } ); this.transitionToRoute('login'); this.get('flashMessages').information('Please check your email'); }, diff --git a/app/services/ajax.js b/app/services/ajax.js new file mode 100644 index 0000000..a0a8cef --- /dev/null +++ b/app/services/ajax.js @@ -0,0 +1,23 @@ +import Ember from 'ember'; +import AjaxService from 'ember-ajax/services/ajax'; + +const { computed, inject: { service } } = Ember; + +export default AjaxService.extend({ + session: service(), + globals: service(), + + host: computed('globals.apiURL', function() { + return `${this.get('globals.apiURL')}/api/${this.get('globals.genus')}`; + }), + + headers: computed('session.authToken', { + get: function() { + let headers = {}; + this.get('session').authorize('authorizer:application', (headerName, headerValue) => { + headers[headerName] = headerValue; + }); + return headers; + } + }) +}); diff --git a/app/utils/ajax-request.js b/app/utils/ajax-request.js deleted file mode 100644 index a4c8de9..0000000 --- a/app/utils/ajax-request.js +++ /dev/null @@ -1,20 +0,0 @@ -import Ember from 'ember'; - -export default function ajaxRequest(url, options, session) { - return new Ember.RSVP.Promise(function(resolve, reject) { - options = options || {}; - options.url = url; - session.authorize('authorizer:application', (headerName, headerValue) => { - let authHeader = {}; - authHeader[headerName] = headerValue; - options.headers = authHeader; - }); - options.success = function(data) { - resolve(data); - }; - options.error = function(jqXHR, status, error) { - reject(jqXHR, status, error); - }; - Ember.$.ajax(options); - }); -} diff --git a/package.json b/package.json index 6925274..45d3b65 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "ember-cli-flash": "1.3.6", "ember-cli-htmlbars": "^1.0.1", "ember-cli-htmlbars-inline-precompile": "^0.3.1", - "ember-cli-ic-ajax": "0.2.1", + "ember-ajax": "0.7.0", "ember-cli-inject-live-reload": "^1.3.1", "ember-cli-mirage": "0.1.11", "ember-cli-qunit": "^1.0.4",