Repository navigation
Expand file tree
/
Copy pathSprintModel.js
More file actions
88 lines (77 loc) · 3.21 KB
/
Copy pathSprintModel.js
File metadata and controls
88 lines (77 loc) · 3.21 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
78
79
80
81
82
83
84
85
86
87
88
/**
* Sprint service that maps sails.js and angular.js together. Note that this service
* handles all 'sprint' 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('SprintModel',
[
'$sailsSocket', 'DataService', '_',
function($sailsSocket, DataService, _) {
var endpoint = 'sprint';
var sprint = {};
var sprints = [];
var handlers = {};
// Add handler for 'created' event
handlers.created = function(sprint) {
sprints.push(sprint);
};
// Handler for 'updated' event
handlers.updated = function(sprint) {
handlers.removed(sprint);
handlers.created(sprint);
};
// Handler for 'removed' event
handlers.removed = function(sprint) {
_.remove(sprints, function(sprintOld) {
return sprintOld.id === sprint.id;
});
};
// Subscribe to 'sprint' 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 sprints from server
function load(parameters) {
return DataService
.collection(endpoint, parameters)
.then(function(response) {
sprints = response.data;
return sprints;
});
}
// Fetch one sprint from server
function fetch(identifier, parameters) {
return DataService
.fetch(endpoint, identifier, parameters)
.then(function(response) {
sprint = response.data;
return sprint;
});
}
// Return count of sprints
function count(parameters) {
return DataService
.count(endpoint, parameters)
.then(function(response) {
return response.data;
});
}
return {
load: load,
fetch: fetch,
count: count
};
}
]
);
}());