UNPKG

7.65 kBJavaScriptView Raw
1'use strict';
2
3exports.__esModule = true;
4exports.Model = Model;
5exports.getActionTypes = getActionTypes;
6exports.getModels = getModels;
7exports.emptyModels = emptyModels;
8
9function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
10
11var _gfsReactReduxTwowayBinding = require('gfs-react-redux-twoway-binding');
12
13var _immutable = require('immutable');
14
15var _immutable2 = _interopRequireDefault(_immutable);
16
17/**
18 * 实体、数据模型,model中的方法和属性都该设置成静态类型
19 * @class Model
20 * */
21//需要一个队列保存model
22var __gfs_mvc_m_list = {};
23var __gfs_mvc_m_actiontypes = {};
24
25var DEFAULT_METHOD_FIX = '$$';
26exports.DEFAULT_METHOD_FIX = DEFAULT_METHOD_FIX;
27var DEFAULT = 'default';
28
29exports.DEFAULT = DEFAULT;
30function getField(data, path) {
31 try {
32 var newpath = path.concat();
33 for (var i = 0, len = newpath.length, p, v; i < len; i++) {
34 //如果path不是最后一个
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
52function 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
62function 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
72var 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
111function 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 //__gfs_mvc_m_actiontypes[`${fix}${modelName}${DEFAULT_METHOD_FIX}${item}`] = `${fix}${modelName}${DEFAULT_METHOD_FIX}}${item}`
129 }
130 }
131
132 return {
133 property: property,
134 method: method
135 };
136}
137
138/**
139 * 一个类装饰器,被装饰的类会变成store,默认不需要额外提供对数据操作的方法,在control中默认会提供del、update、insert等数据操作方法;如果有特殊需求无法满足使用场景可按照example中给出的方式自行编写数据处理方法<br />
140 * <strong style="color:red">注意:model类中`__name`属性必须要有,这是为了能在各个component正常使用model必备的一个属性,必须小写,默认会在字符串后面添加上"model",例如:`static __name='test'`,那么在实际中运用应该是this.props.testmodel</strong>
141 * @method Model
142 * @param target {object} 被包装的对象
143 * @example
144 *
145 * import {Model} from 'gfs-react-mvc'
146 * //这里由于@为文档关键符号,所以下面将以$代替
147 * //@Model
148 * $Model
149 class TestModel {
150 //__name必须要有,这是为了能再各个component正常使用model必备的一个属性,必须小写
151 static __name = 'testmodel'
152 //数据模型
153 static age = 20
154 static xq = {}
155 //可以自行定义数据操作方法,在control中通过
156 //dispatch({
157 // type:`testmodel$$save`,
158 // data:data
159 //})
160 //这种方式变更数据,其中type值的组成是通过:类名(全小写)+ 方法名组成
161 static save(data, action){
162 if(action.data){
163 return data.merge(Immutable.fromJS(action.data) )
164 }
165 }
166 //dispatch({
167 // type:`testmodel$$del`,
168 // data:data
169 //})
170 static del(data, action){
171 if(action.data){
172 return data.merge(Immutable.fromJS(action.data) )
173 }
174 }
175 }
176 * */
177
178function 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 // params = implement(target.prototype,modelName,params)
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
208function getActionTypes(typeName) {
209 return __gfs_mvc_m_actiontypes[typeName];
210}
211
212function getModels() {
213 return __gfs_mvc_m_list;
214}
215
216function emptyModels() {
217 __gfs_mvc_m_list = null;
218 //delete __gfs_mvc_m_list
219}
\No newline at end of file