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; return;
} }
var self = this;
$("#newUserFirstname").removeClass("error"); $("#newUserFirstname").removeClass("error");
$("#newUserLastname").removeClass("error"); $("#newUserLastname").removeClass("error");
$("#newUserEmail").removeClass("error"); $("#newUserEmail").removeClass("error");
this.get('userService').add(this.get('newUser')).then(function(/*user*/) { this.get('userService')
self.showNotification('Added'); .add(this.get('newUser'))
self.set('newUser', { firstname: "", lastname: "", email: "", active: true }); .then((user) => {
$("#newUserFirstname").focus(); this.showNotification('Added');
this.set('newUser', { firstname: "", lastname: "", email: "", active: true });
self.get('userService').getAll().then(function(users) { $("#newUserFirstname").focus();
self.set('model', users); 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) { onDelete(user) {

View file

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

View file

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