forked from dperrymorrow/example-backbone-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.js
60 lines (47 loc) · 1.54 KB
/
store.js
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
/* implements a non-persistent store in memory */
module.exports = function(){
var fields = new Array();
this.get = function(field, id, callback){
//Parsing arguments
if(typeof(callback) == 'undefined'){ callback = function(){}; }
if(typeof(id) == 'function'){
id(fields[field]); //id is the callback
}
if(typeof(id) == 'undefined'){
callback(fields[field]);
}
if(hasField(field)){
callback(fields[field][id]);
} else {
callback(null);
}
}
this.put = function(field, value, callback){
if(!hasField(field))
fields[field] = new Array();
fields[field].push(value);
if(typeof(callback) === 'function')
callback(fields[field].length - 1);
}
this.set = function(field, id, value, callback){
if(!hasField(field))
fields[field] = new Array();
fields[field][id] = value;
if(typeof(callback) === 'function')
callback();
}
this.destroy = function(field, id, callback){
if(hasField(field)){
if(fields[field].length == 1 && id in fields[field]){
delete fields[field];
} else {
fields[field].splice(id, 1);
}
}
if(typeof(callback) === 'function')
callback();
}
function hasField(field){
return typeof(fields[field]) !== 'undefined';
}
};