-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.js
90 lines (75 loc) · 2.64 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
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
'use strict';
var {alt, Actions, PageStore} = require('react-native-router-flux');
var FormActions = require('./actions');
var React = require('react-native');
var {
AsyncStorage
} = React;
var STORAGE_KEY = 'FormStorageKey';
class FormStore {
constructor(){
this.all = {};
this.bindAction(FormActions.set, this.onSet);
this.bindAction(FormActions.clear, this.onClear);
this.bindAction(Actions.init, this.onPageOpened);
this.bindAction(Actions.push, this.onPageOpened);
this.bindAction(Actions.custom, this.onCustom);
this.bindAction(Actions.pop, this.onPageClosed);
this.bindAction(Actions.dismiss, this.onPageClosed);
}
onPageOpened(data){
this.waitFor(PageStore.dispatchToken);
this.route = PageStore.getState().currentRoute;
if (!this.route){
console.error("No current route!");
}
// console.log("onPageOpened"+JSON.stringify(data)+" route "+ this.route);
// load data for current route
this.all[this.route] = {};
AsyncStorage.getItem(STORAGE_KEY+this.route, (error, result)=> this.onLoad(result));
return false;
}
onCustom({data}){
this.onSet(data||{});
}
onPageClosed(data){
console.log("onPageClosed"+data+" route "+ this.route);
// store data for prev route
this.onSet(data || {});
// change current route
this.waitFor(PageStore.dispatchToken);
this.route = PageStore.getState().currentRoute;
if (!this.route){
console.error("No current route!");
}
// store data for new current route
this.onSet(data || {});
return false;
}
onSet(map){
if (!this.all[this.route]){
this.all[this.route]={};
}
console.log("FormStore.onSet: "+JSON.stringify(map)+" for route:"+this.route+" DATA"+JSON.stringify(this.all[this.route]));
for (var key in map){
this.all[this.route][key] = map[key];
}
AsyncStorage.setItem(STORAGE_KEY+this.route, JSON.stringify(this.all[this.route]||{}));
this.setState({data:this.all[this.route]});
}
onLoad(data){
if (data) {
console.log("onPageOpened loaded "+data+" route "+ this.route);
data = JSON.parse(data);
for (var key in data){
this.all[this.route][key] = data[key];
}
this.setState({data:this.all[this.route]});
}
}
onClear(){
AsyncStorage.removeItem(STORAGE_KEY+this.route);
this.setState({data:{}});
}
}
module.exports = alt.createStore(FormStore);