UNPKG

2.27 kBJavaScriptView Raw
1//import {bindingMixin} from 'gfs-react-redux-twoway-binding'
2'use strict';
3
4exports.__esModule = true;
5exports.View = View;
6
7function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
8
9var _reactRedux = require('react-redux');
10
11var _extend = require('extend');
12
13var _extend2 = _interopRequireDefault(_extend);
14
15/**
16 * 视图
17 * @class View
18 * */
19/**
20 * 一个装饰器方法,用于装饰类,被装饰的类为页面视图,或者说是react的component,并不是每一个component都需要被装饰
21 * @method View
22 * @param action {object} control对象,可以是一个数组,比如:View([TestControl,Test2Control] )
23 * @return class
24 * @example
25 *
26 * import {View} from 'gfs-react-mvc'
27 * import TestControl from './TestControl'
28 * //这里由于@为文档关键符号,所以下面将以$代替
29 * //@View(TestControl)
30 * $View(TestControl)
31 class TestComponent extends Component {
32 constructor(props) {
33 super(props)
34 }
35
36 componentDidMount(){
37 setTimeout(()=>{
38 //调用control中的action
39 this.props.save(this)
40 },1000)
41 }
42
43 static defaultProps={}
44
45 render() {
46 console.log('age:',this.props.testmodel.get('age') )
47 return (
48 <div>
49 {this.props.testmodel.get('age')}
50 </div>
51 )
52 }
53 }
54 * */
55
56function View(actions) {
57
58 return function (target) {
59
60 if (actions) {
61
62 actions = Array.prototype.concat.call([], actions);
63 var controls = {};
64 for (var i = 0, len = actions.length; i < len; i++) {
65 controls = _extend2['default'](controls, actions[i].controls);
66 }
67 return _reactRedux.connect(function (state) {
68 var stories = {};
69 for (var i = 0, len = actions.length; i < len; i++) {
70 stories[actions[i].modelName] = state[actions[i].modelName];
71 }
72 return stories;
73 }, controls || {})(target);
74 }
75
76 return target;
77 };
78}
\No newline at end of file