-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStoarray.js
More file actions
147 lines (113 loc) · 3.06 KB
/
Stoarray.js
File metadata and controls
147 lines (113 loc) · 3.06 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
window.Stoarray = (function(){
//private variables
var _name,_autosave,_events;
var _eventsCallback = {};
var _addedMethods = []
//constructor
var Stoarray = function(name,opts){
_name = name;
_autosave = (opts.autosave === undefined)? true : !!opts.autosave;
_events = (opts.events === undefined)? true : !!opts.events;
var ignore = opts.ignore;
if(opts.debug){
ignore = true;
_autosave = false;
}
_initData.call(this,opts.defaultValues,ignore);
_save.call(this);
return this;
}
//private method
function _initData(d,ignore){
var fromStorage = (ignore)?null:JSON.parse(localStorage.getItem(_name));
this.data = fromStorage || d || [];
return this;
}
function _save(){
if(_events){
_triggerEvent('change', this.data );
}
if(_autosave){
console.log('autosave')
this.save();
}
}
//array data methods
Stoarray.prototype.use = function(method,fn,autre){
var newData = this.data[method](fn.bind(this),autre);
var newStoarray = new Stoarray(_name, {
autosave : false,
events : _events,
ignore : true,
defaultValues : newData
});
_addedMethods.forEach(function(obj){
newStoarray.addMethod(obj.name,obj.method)
})
return newStoarray;
}
Stoarray.prototype.each = function(fn){
return this.use('forEach',fn);
}
Stoarray.prototype.push = function(data){
if(!(data instanceof Array)){
data = [data];
}
for(var i = 0; i<data.length;i++){
this.data.push(data[i]);
}
_save.call(this);
_triggerEvent('push',this.data, data );
return this;
}
Stoarray.prototype.map = function(fn){
return this.use('map',fn);
}
Stoarray.prototype.reduce = function(fn,a){
return this.use('reduce',fn,a);
}
//localStorage methods
Stoarray.prototype.save = function(){
var dataString = JSON.stringify(this.data);
localStorage.setItem(_name,dataString);
_triggerEvent('saved',this.data);
return this;
}
//helper methods
Stoarray.prototype.log = function(){
console.log(this.data);
return this;
}
Stoarray.prototype.clean = function(){
this.data = [];
this.save();
return this;
}
Stoarray.prototype.addMethod = function(name,method){
_addedMethods.push({name:name,method,method})
this[name] = function(){
method.apply(this,arguments);
_save.call(this);
return this;
}.bind(this);
return this;
}
//events methods
Stoarray.prototype.on = function(eventName,callback){
if(_eventsCallback[eventName] && _eventsCallback[eventName] instanceof Array){
_eventsCallback[eventName].push(callback)
}else{
_eventsCallback[eventName] = [callback]
}
return this;
}
function _triggerEvent(eventName,data,changed){
if(!_events) return;
if(_eventsCallback[eventName] && _eventsCallback[eventName].length >0){
for(var i = 0; i<_eventsCallback[eventName].length;i++){
_eventsCallback[eventName][i].call(null,data,changed)
}
}
}
return Stoarray;
})();