[原创] react初探(二)

react reflux html5upload js

2015-08-15 37388

使用react+reflux实现多图片上传,主要是针对手机端的。因为手机端无法使用flash插件,建议使用jquery.html5uploader.min.js这个插件。

再次进行鄙视,找了那么多cdn居然没有reflux可用。只能自己bower一个在本地安装使用。以下是主要代码,使用jsx编译后的

效果截图如下


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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<!DOCTYPE html>
<html lang="en">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>reflux-demo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
  <meta name="apple-mobile-web-app-capable" content="yes" /><meta name="apple-mobile-web-app-status-bar-style" content="black" />
  <meta name="Keywords" content="reflux-demo" /><meta name="Description" content="reflux-demo" />
  <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css"/>
  <script type="text/javascript" src="http://cdn.bootcss.com/jquery/2.1.0/jquery.js"></script>
 
  <script type="text/javascript" src="http://cdn.bootcss.com/underscore.js/1.8.3/underscore-min.js"></script>
 
 
  <script src="http://cdn.bootcss.com/react/0.13.3/react.js"></script>
  <!--<script src="http://cdn.bootcss.com/react-router/0.13.3/ReactRouter.js"></script>-->
  <script type="text/javascript" src="http://localhost:8081/js/bower_components/reflux/dist/reflux.js"></script>
  <style type="text/css">
      #single_image{width: 0px;height: 0px;}
      .qingjian_make{ overflow:hidden;border-top:2px solid #fff; color:#FFFFFF}
      .qingjian_table td input{ border:0p; border:0; height:50px; line-height:50px; color:#fff; background:none; width:100%}
      .qingjian_table td{ border-bottom:1px solid #FFFFFF; padding-left:5px}
      .qingjian_add{ padding-bottom:8px;height: auto;}
      .qingjian_add h3{ font-weight:100; font-size:14px; line-height:30px}
      .qingjian_add ul li{ width:80px; height:80px; overflow:hidden; display:inline; margin-right:10px; overflow:hidden; float:left; margin-bottom:10px; position:relative}
      .qingjian_add ul li a{ width:80px; max-height:80px; float:left}
      .qingjian_add ul li img{width:80px; height:80px;}
      .qingjian_add ul li a.my-close{ position:absolute; right:1px; top:1px; width:10px; height:20px; font-size:14px; color:#fff}
      .qingjian_addbtn{ height:30px; line-height:30px; background:#F1549B; text-align:center; color:#fff; font-size:14px; text-align:center; float:left; width:100%; margin:10px 0px;-moz-border-radius:2px;-webkit-border-radius:2px;border-radius:2px;}
  </style>
 
 
 
</head>
 
 
 
<body class="bg01">
 
<div class="qingjian_make">
  <div class="qingjian_add">
    <div class="clear"></div>
    <div id="manyImgCtrls">
 
    </div>
 
  </div>
 
  <!--<textarea ng-model="remarks" style="height: 50px;" placeholder="此刻的想法......"></textarea>-->
  <!--<div class="clear"></div>-->
  <!--<a class="qingjian_btn" href="javascript:void(0)" ng-click="sub_form($event.target)">保存并分享</a>-->
</div>
 
<div class="container">
  <input id="single_image" type="file" name="image"/>
  <div id="main_container"></div>
 
</div>
 
</body>
<script>
    /**
     * Created by yuluo on 15/8/15.
     */
    var ManyImgsActions = Reflux.createActions([
        'getAll',
        'addImg',
        'removeImg'
    ]);
 
    var ManyImgsStore = Reflux.createStore({
        items: [],
        listenables: [ManyImgsActions],
        onGetAll: function () {
            this.trigger(this.items);
        },
        onAddImg:function(url){
            this.items.push(url);
            this.trigger(this.items);
        },
        onRemoveImg:function(url){
            var find_item = _.find(this.items, function (item) {
                return item == url;
            });
            this.items = _.difference(this.items, [find_item]);/////删除指定的元素
            this.trigger(this.items);
        }
    });
    var ManyImgsComponent = React.createClass({displayName: "ManyImgsComponent",
        mixins: [Reflux.connect(ManyImgsStore, 'list')],
        getInitialState: function () {
            $("#single_image").html5Uploader({
                name:"Filedata",
                postUrl: "/common/uploadfile",
                onSuccess:function(msg){
                    try{
                        var url = JSON.parse(msg.currentTarget.response).url;;
                        ManyImgsActions.addImg(url);
                    }
                    catch(e){
                        console.log(e);
                    }
                }
            });
            return {list: []};
        },
        onStatusChange: function (list) {
            //this.setState({list: list});
        },
        componentDidMount: function () {
            //this.unsubscribe = ManyImgsStore.listen(this.onStatusChange);
            ManyImgsActions.getAll();
        },
        componentWillUnmount: function () {
            this.unsubscribe();
        },
        handleUploadImg:function(){
            $("#single_image").click();
 
            //console.log(this.props);
        },
        render: function () {
            return (
                    React.createElement("ul", null,
 
 
                            this.state.list.map(function(item){
                                return(
                                        React.createElement(ImgItemComponent, {item: item})
                                )
                            }),
                            React.createElement("li", null, React.createElement("a", {onClick: this.handleUploadImg, className: "qingjian_addbtn", href: "javascript:void(0)"}, "+"))
                    )
            )
        }
    });
 
    var ImgItemComponent = React.createClass({displayName: "ImgItemComponent",
        handleDeleteImg:function(item){
            //console.log(this.props);
            ManyImgsActions.removeImg(this.props.item);
        },
        render:function(){
            return(
                    React.createElement("li", null,
                            React.createElement("a", {href: "javascript:void(0)"}, React.createElement("img", {src: this.props.item})),
                            React.createElement("a", {className: "my-close", href: "javascript:void(0)", onClick: this.handleDeleteImg}, "×")
                    )
            )
        }
    });
 
    React.render(React.createElement(ManyImgsComponent, null), document.getElementById("manyImgCtrls"));
</script>
</html>