Repository navigation
Expand file tree
/
Copy pathUserModel.js
More file actions
77 lines (68 loc) · 2.76 KB
/
Copy pathUserModel.js
File metadata and controls
77 lines (68 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* User service that maps sails.js and angular.js together. Note that this service
* handles all 'user' specified CRUD operations.
*
* Basically there is similar service model for all sails.js objects and usage of those
* all are the same.
*/
(function() {
'use strict';
angular.module('Taskboard.services')
.factory('UserModel',
[
'$sailsSocket', 'DataService',
function($sailsSocket, DataService) {
var endpoint = 'user';
var user = {};
var users = [];
var handlers = {};
// Subscribe to 'user' endpoint and attach event handlers to it
$sailsSocket
.subscribe(endpoint, function(message) {
if (handlers[message.verb]) {
handlers[message.verb](message);
} else {
console.log('Implement handling for \'' + message.verb + '\' socket messages');
}
});
// Load users from server
function load(parameters) {
return DataService
.collection(endpoint, parameters)
.then(function(response) {
users = response.data;
return users;
});
}
// Fetch one user from server
function fetch(identifier, parameters) {
return DataService
.fetch(endpoint, identifier, parameters)
.then(function(response) {
user = response.data;
return user;
});
}
// Return count of users
function count(parameters) {
return DataService
.count(endpoint, parameters)
.then(function(response) {
return response;
});
}
// Update of specified user
function update(identifier, data) {
return DataService
.update(endpoint, identifier, data);
}
return {
load: load,
fetch: fetch,
count: count,
update: update
};
}
]
);
}());