-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
112 lines (101 loc) · 3.72 KB
/
store.go
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
package main
import (
"fmt"
"strings"
)
type Store struct {
ResourceOptions
}
func (s *Store) GetDir() string {
return "stores"
}
func (s *Store) GetFileName() string {
return s.ResourceOptions.Domain + "-store.js"
}
func (s *Store) GetFileContent() string {
domainFirstCharUpper := strings.ToUpper(s.Domain[:1]) + s.Domain[1:]
return fmt.Sprintf(s.getTemplate(), domainFirstCharUpper, s.Domain, s.Constant)
}
func (s *Store) getTemplate() string {
content := s.getHeaderTemplate()
content = content + s.getBodyVariationTemplate()
content = content + s.getFooterTemplate()
return content
}
func (s *Store) getHeaderTemplate() string {
return "var AppDispatcher = require('../dispatcher/app-dispatcher');\n" +
"var EventEmitter = require('events').EventEmitter;\n" +
"var assign = require('object-assign');\n" +
"var %[1]sConstants = require('../constants/%[2]s-constants');\n" +
"var CHANGE_EVENT = 'change';\n\n"
}
func (s *Store) getBodyVariationTemplate() string {
if s.IsApi {
return "" +
"var _data = {\n" +
" %[2]ss: null\n" +
"};\n\n" +
"var %[1]sStore = assign({}, EventEmitter.prototype, {\n" +
" getData: function() {\n" +
" return _data;\n" +
" },\n\n" +
" get%[1]ss: function() {\n" +
" return _data.%[2]ss;\n" +
" },\n\n" +
" emitChange: function() {\n" +
" this.emit(CHANGE_EVENT);\n" +
" },\n\n" +
" addChangeListener: function(callback) {\n" +
" this.on(CHANGE_EVENT, callback);\n" +
" },\n\n" +
" removeChangeListener: function(callback) {\n" +
" this.removeListener(CHANGE_EVENT, callback);\n" +
" }\n" +
"});\n\n" +
"AppDispatcher.register(function(action) {\n" +
" switch (action.actionType) {\n" +
" case %[1]sConstants.%[3]s_SUCCESS:\n" +
" _data.%[2]ss = action.%[2]ss;\n" +
" %[1]sStore.emitChange();\n" +
" break;\n\n" +
" default:\n" +
" // no op\n" +
" }\n" +
"});\n\n"
} else {
return "" +
"var _data = {\n" +
" %[2]s: null\n" +
"};\n\n" +
"var %[1]sStore = assign({}, EventEmitter.prototype, {\n" +
" getData: function() {\n" +
" return _data;\n" +
" },\n\n" +
" get%[1]s: function() {\n" +
" return _data.%[2]s;\n" +
" },\n\n" +
" emitChange: function() {\n" +
" this.emit(CHANGE_EVENT);\n" +
" },\n\n" +
" addChangeListener: function(callback) {\n" +
" this.on(CHANGE_EVENT, callback);\n" +
" },\n\n" +
" removeChangeListener: function(callback) {\n" +
" this.removeListener(CHANGE_EVENT, callback);\n" +
" }\n" +
"});\n\n" +
"AppDispatcher.register(function(action) {\n" +
" switch (action.actionType) {\n" +
" case %[1]sConstants.%[3]s:\n" +
" _data.%[2]s = action.%[2]s;\n" +
" %[1]sStore.emitChange();\n" +
" break;\n\n" +
" default:\n" +
" // no op\n" +
" }\n" +
"});\n\n"
}
}
func (s *Store) getFooterTemplate() string {
return "module.exports = %[1]sStore;"
}