1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-08-07 22:45:24 +02:00

Install ember-ajax and refactor add method in user service and controller

This commit is contained in:
zinyando 2016-05-25 21:10:28 +02:00
parent 6e71dbc234
commit 3f0d98a5b0
3 changed files with 27 additions and 55 deletions

View file

@ -20,23 +20,22 @@ export default Ember.Controller.extend(NotifierMixin, {
return;
}
var self = this;
$("#newUserFirstname").removeClass("error");
$("#newUserLastname").removeClass("error");
$("#newUserEmail").removeClass("error");
this.get('userService').add(this.get('newUser')).then(function(/*user*/) {
self.showNotification('Added');
self.set('newUser', { firstname: "", lastname: "", email: "", active: true });
$("#newUserFirstname").focus();
self.get('userService').getAll().then(function(users) {
self.set('model', users);
this.get('userService')
.add(this.get('newUser'))
.then((user) => {
this.showNotification('Added');
this.set('newUser', { firstname: "", lastname: "", email: "", active: true });
$("#newUserFirstname").focus();
this.get('model').pushObject(user);
})
.catch(function(){
let msg = error.status === 409 ? 'Unable to add duplicate user' : 'Unable to add user';
self.showNotification(msg);
});
}, function(error) {
let msg = error.status === 409 ? 'Unable to add duplicate user' : 'Unable to add user';
self.showNotification(msg);
});
},
onDelete(user) {

View file

@ -14,24 +14,18 @@ import models from '../utils/model';
export default Ember.Service.extend({
sessionService: Ember.inject.service('session'),
ajax: Ember.inject.service(),
// Adds a new user.
add(user) {
let url = this.get('sessionService').appMeta.getUrl(`users`);
return new Ember.RSVP.Promise(function(resolve, reject) {
$.ajax({
url: url,
type: 'POST',
data: JSON.stringify(user),
contentType: 'json',
success: function(response) {
resolve(models.UserModel.create(response));
},
error: function(reason) {
reject(reason);
}
});
return this.get('ajax').request(url, {
type: 'POST',
data: JSON.stringify(user),
contentType: 'json'
}).then(function(response){
return models.UserModel.create(response);
});
},
@ -57,21 +51,9 @@ export default Ember.Service.extend({
getAll() {
let url = this.get('sessionService').appMeta.getUrl(`users`);
return new Ember.RSVP.Promise(function(resolve, reject) {
$.ajax({
url: url,
type: 'GET',
success: function(response) {
let data = [];
_.each(response, function(obj) {
data.pushObject(models.UserModel.create(obj));
});
resolve(data);
},
error: function(reason) {
reject(reason);
}
return this.get('ajax').request(url).then((response) => {
return response.map(function(obj){
return models.UserModel.create(obj);
});
});
},
@ -104,19 +86,10 @@ export default Ember.Service.extend({
let userId = user.get('id');
let url = this.get('sessionService').appMeta.getUrl(`users/${userId}`);
return new Ember.RSVP.Promise(function(resolve, reject) {
$.ajax({
url: url,
type: 'PUT',
data: JSON.stringify(user),
contentType: 'json',
success: function(response) {
resolve(response);
},
error: function(reason) {
reject(reason);
}
});
return this.get('ajax').request(url, {
type: 'PUT',
data: JSON.stringify(user),
contentType: 'json'
});
},
@ -209,4 +182,4 @@ export default Ember.Service.extend({
});
});
}
});
});

View file

@ -20,7 +20,7 @@
"license": "MIT",
"devDependencies": {
"broccoli-asset-rev": "^2.4.2",
"ember-ajax": "0.7.1",
"ember-ajax": "2.3.2",
"ember-cli": "2.5.0",
"ember-cli-app-version": "^1.0.0",
"ember-cli-babel": "^5.1.6",