1 | 'use strict';
|
2 |
|
3 | exports.__esModule = true;
|
4 | exports.Model = Model;
|
5 | exports.getActionTypes = getActionTypes;
|
6 | exports.getModels = getModels;
|
7 | exports.emptyModels = emptyModels;
|
8 |
|
9 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
|
10 |
|
11 | var _gfsReactReduxTwowayBinding = require('gfs-react-redux-twoway-binding');
|
12 |
|
13 | var _immutable = require('immutable');
|
14 |
|
15 | var _immutable2 = _interopRequireDefault(_immutable);
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | var __gfs_mvc_m_list = {};
|
23 | var __gfs_mvc_m_actiontypes = {};
|
24 |
|
25 | var DEFAULT_METHOD_FIX = '$$';
|
26 | exports.DEFAULT_METHOD_FIX = DEFAULT_METHOD_FIX;
|
27 | var DEFAULT = 'default';
|
28 |
|
29 | exports.DEFAULT = DEFAULT;
|
30 | function getField(data, path) {
|
31 | try {
|
32 | var newpath = path.concat();
|
33 | for (var i = 0, len = newpath.length, p, v; i < len; i++) {
|
34 |
|
35 | if (i != len) {
|
36 | p = newpath.slice(0, i + 1);
|
37 | v = data.getIn(p);
|
38 | if (!v) {
|
39 | var _Immutable$fromJS;
|
40 |
|
41 | data = typeof v == 'undefined' ? data.mergeIn(p.slice(0, p.length - 1), _immutable2['default'].fromJS((_Immutable$fromJS = {}, _Immutable$fromJS[p[p.length - 1]] = {}, _Immutable$fromJS))) : data.setIn(p, {});
|
42 | }
|
43 | }
|
44 | }
|
45 | } catch (ex) {
|
46 | console && console.warn && console.warn(ex);
|
47 | }
|
48 |
|
49 | return data;
|
50 | }
|
51 |
|
52 | function merger(prev, next) {
|
53 | if (_immutable2['default'].List.isList(prev) && _immutable2['default'].List.isList(next)) {
|
54 | return next;
|
55 | }
|
56 | if (prev && prev.mergeWith) {
|
57 | return prev.mergeWith(merger, next);
|
58 | }
|
59 | return next;
|
60 | }
|
61 |
|
62 | function getOriginData(data) {
|
63 | if (data) {
|
64 | if (typeof data.toJS == 'undefined') {
|
65 | return _immutable2['default'].fromJS(data);
|
66 | }
|
67 | }
|
68 |
|
69 | return data;
|
70 | }
|
71 |
|
72 | var curl = {
|
73 | del: function del(data, action) {
|
74 | return getOriginData(data).deleteIn(action.path);
|
75 | },
|
76 | update: function update(data, action) {
|
77 | data = getOriginData(getField(data, action.path));
|
78 | if (typeof action.data === 'string' && action.path) {
|
79 | return data.setIn(action.path, action.data);
|
80 | }
|
81 | action.data = _immutable2['default'].fromJS(action.data);
|
82 | return action.path ? data.mergeDeepIn(action.path, action.data) : data.mergeDeep(action.data);
|
83 | },
|
84 | updateWith: function updateWith(data, action) {
|
85 | data = getOriginData(data);
|
86 | if (typeof action.data === 'string' && action.path) {
|
87 | return data.setIn(action.path, action.data);
|
88 | }
|
89 | action.data = _immutable2['default'].fromJS(action.data);
|
90 | return data.mergeWith(action.merge ? action.merge : merger, action.data);
|
91 | },
|
92 | save: function save(data, action) {
|
93 | data = getOriginData(getField(data, action.path));
|
94 | return data.setIn(action.path, action.isImmutable ? _immutable2['default'].fromJS(action.data) : action.data);
|
95 | },
|
96 | insert: function insert(data, action) {
|
97 | data = getOriginData(getField(data, action.path));
|
98 | return data.setIn(action.path, action.isImmutable ? _immutable2['default'].fromJS(action.data) : action.data);
|
99 | },
|
100 | query: function query(data) {
|
101 | return data;
|
102 | },
|
103 | findOne: function findOne(data, action) {
|
104 | return getOriginData(data).getIn(action.path);
|
105 | },
|
106 | find: function find(data, action) {
|
107 | return getOriginData(data).getIn(action.path);
|
108 | }
|
109 | };
|
110 |
|
111 | function implement() {
|
112 | var target = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
|
113 | var modelName = arguments.length <= 1 || arguments[1] === undefined ? '' : arguments[1];
|
114 | var param = arguments.length <= 2 || arguments[2] === undefined ? { property: {}, method: {} } : arguments[2];
|
115 | var fix = arguments.length <= 3 || arguments[3] === undefined ? '' : arguments[3];
|
116 |
|
117 | var property = param.property;
|
118 | var method = param.method;
|
119 |
|
120 | if (fix) {
|
121 | fix += DEFAULT_METHOD_FIX;
|
122 | }
|
123 | for (var item in target) {
|
124 | if (!(target[item] instanceof Function)) {
|
125 | property[item] = target[item];
|
126 | } else {
|
127 | method['' + fix + modelName + DEFAULT_METHOD_FIX + item] = target[item].bind(target);
|
128 |
|
129 | }
|
130 | }
|
131 |
|
132 | return {
|
133 | property: property,
|
134 | method: method
|
135 | };
|
136 | }
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 | function Model(target) {
|
179 | var params = {};
|
180 |
|
181 | if (typeof target.__name == 'undefined') {
|
182 | console && console.warn('[create model error] ', 'Model中必须存在__name属性,并赋予store名称,例如: static __name="testmodel"');
|
183 | return {
|
184 | modelName: '',
|
185 | store: null
|
186 | };
|
187 | }
|
188 | var modelName = target.__name.toLowerCase();
|
189 |
|
190 | if (modelName.indexOf('model') <= -1) {
|
191 | modelName += 'model';
|
192 | }
|
193 |
|
194 | params = implement(target, modelName);
|
195 | params = implement(curl, modelName, params, DEFAULT);
|
196 |
|
197 |
|
198 | var store = _gfsReactReduxTwowayBinding.createReducer(modelName, _immutable2['default'].fromJS(params.property || {}), params.method);
|
199 |
|
200 | __gfs_mvc_m_list['' + modelName] = store;
|
201 |
|
202 | return {
|
203 | modelName: modelName,
|
204 | store: store
|
205 | };
|
206 | }
|
207 |
|
208 | function getActionTypes(typeName) {
|
209 | return __gfs_mvc_m_actiontypes[typeName];
|
210 | }
|
211 |
|
212 | function getModels() {
|
213 | return __gfs_mvc_m_list;
|
214 | }
|
215 |
|
216 | function emptyModels() {
|
217 | __gfs_mvc_m_list = null;
|
218 |
|
219 | } |
\ | No newline at end of file |