Skip to content
This repository was archived by the owner on Sep 8, 2020. It is now read-only.

Commit f3655b0

Browse files
authored
Update UserObject.js
1 parent 8641d3c commit f3655b0

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

modules/User/UserObject.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,45 @@
11
var module = angular.module('App.User');
22
module.factory( 'User', (BaseObject, $http) => {
33
class User extends BaseObject {
4+
/**
5+
* Get list of users
6+
*
7+
* @returns {Promise<User[]>}
8+
*/
49
static list() {
510
return $http.get('/api/users')
6-
.then( (response) => response.data.map(User.new));
11+
.then( (response) => response.data.map( user => new User(user) ));
712
}
813

914

10-
// checks validity of the property (probably should be in BaseObject)
15+
/**
16+
* Validity function for use in the view / form
17+
*
18+
* @note Client-side validation is a convenience, server-side validation is imperative
19+
*/
1120
validate(property) {
1221
if (!User.rules[property])
1322
return true;
1423

1524
var pattern = new RegExp(User.rules[property]);
1625
return pattern.test(this[property]);
1726
}
27+
28+
create() {
29+
return this.cache('creating', () =>
30+
$http.post('/api/users', this)
31+
// Attach additional data from server to this object
32+
.then( data => Object.assign(this, data) )
33+
);
34+
}
35+
36+
update() {
37+
return this.cache('updating', () =>
38+
$http.put('/api/users', this)
39+
// Attach additional data from server to this object
40+
.then( data => Object.assign(this, data) )
41+
);
42+
}
1843
}
1944

2045
User.rules = {

0 commit comments

Comments
 (0)