blob: 832b9d7917075fa793dbd39e16ea4789cacef0b2 (
plain)
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
|
/**
* ownCloud - News
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Bernhard Posselt <dev@bernhard-posselt.com>
* @copyright Bernhard Posselt 2014
*/
app.factory('Resource', () => {
'use strict';
class Resource {
constructor (id, http) {
this.id = id;
this.values = [];
this.hashMap = {};
this.http = http;
}
receive (values) {
values.forEach((value) => {
this.add(value);
});
}
add (obj) {
let existing = this.hashMap[obj[this.id]];
if (existing === undefined) {
this.values.push(obj);
this.hashMap[obj[this.id]] = obj;
} else {
// copy values from new to old object if it exists already
for (let [key, value] of items(obj)) {
existing[key] = value;
}
}
}
size () {
return this.values.length;
}
get (id) {
return this.hashMap[id];
}
delete (id) {
// find index of object that should be deleted
let deleteAtIndex;
for (let i = 0; i < this.values.length; i += 1) {
if (this.values[i][this.id] === id) {
deleteAtIndex = i;
break;
}
}
if (deleteAtIndex !== undefined) {
this.values.splice(deleteAtIndex, 1);
}
if (this.hashMap[id] !== undefined) {
delete this.hashMap[id];
}
}
clear () {
this.hashMap = {};
// http://stackoverflow.com/questions/1232040
// this is the fastes way to empty an array when you want to keep
// the reference around
while (this.values.length > 0) {
this.values.pop();
}
}
getAll () {
return this.values;
}
}
return Resource;
});
|