自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何在前端編碼時(shí)實(shí)現(xiàn)人肉雙向編譯

開發(fā) 前端
React+flux是目前最火的前端解決方案之一,但flux槽點(diǎn)頗多,例如store比較混亂,使用比較繁瑣等,于是出現(xiàn)了很多第三方的基于flux優(yōu)化的架構(gòu)。

如何在前端編碼時(shí)實(shí)現(xiàn)人肉雙向編譯

React+flux是目前最火的前端解決方案之一,但flux槽點(diǎn)頗多,例如store比較混亂,使用比較繁瑣等,于是出現(xiàn)了很多第三方的基于flux優(yōu)化的架構(gòu)。

有人統(tǒng)計(jì)了目前主流的flux實(shí)現(xiàn)方案,感興趣的可以看這里:Which Flux implementation should I use?

其中redux是目前githubstar最多的一個(gè)方案,該方案完全獨(dú)立于react,意味著這套理念可以作為架構(gòu)層應(yīng)用于其他的組件化方案。同時(shí)官方也提供了react-redux庫,幫助開發(fā)者直接使用react+redux快速開發(fā)。

個(gè)人理解它的主要特性體現(xiàn)在以下幾點(diǎn):

  1. 強(qiáng)制使用一個(gè)全局的storestore只提供了幾個(gè)簡(jiǎn)單的api(實(shí)際上應(yīng)該是4個(gè)),如subscribe/dispatch(訂閱、發(fā)布),getState,replaceReducer

  2. store負(fù)責(zé)維護(hù)一個(gè)唯一的叫做state樹的對(duì)象,其中state存儲(chǔ)了應(yīng)用需要用到的所有數(shù)據(jù)。

  3. store和頂層組件使用connect方法綁定,并賦給props一個(gè)dispatch方法,可以直接在組件內(nèi)部this.props.dispatch(action)。 簡(jiǎn)單一點(diǎn)說,就是去掉了flux中組件和storeunbind/bind環(huán)節(jié)。當(dāng)state變化時(shí),自動(dòng)更新components,不需要手動(dòng)操作。

  4. 提供了applyMiddleware方法用于異步的action,并且提供了加入中間件的能力,例如打印日志追蹤應(yīng)用的所有狀態(tài)變化。

  5. 對(duì)全局的數(shù)據(jù)state的操作,由多個(gè)reducer完成。每個(gè)reducer都是一個(gè)純函數(shù),接收兩個(gè)參數(shù)stateaction,返回處理后的state。這點(diǎn)類似管道的操作。

接下來我們可以回答標(biāo)題的問題了,即:如何在前端編碼時(shí)實(shí)現(xiàn)人肉雙向編(zi)譯(can)。

其實(shí)就是使用coffee來編寫react+redux應(yīng)用。

我們來寫個(gè)簡(jiǎn)單的hello world玩玩。

view部分

這部分和redux/flux無關(guān),純粹react的實(shí)現(xiàn),使用jsx的話,render部分的代碼大概長(zhǎng)這樣:

  1. render:function(){ 
  2.     return ( 
  3.         <div> 
  4.             <div class="timer">定時(shí)器:{interval}</div> 
  5.             <div>{title}</div> 
  6.             <input ref="input"><button>click it.</button> 
  7.         </div> 
  8.     ) 

那如何使用coffee寫這段代碼呢? 我們需要先將jsx編譯這類似這樣的js代碼,請(qǐng)注意是用大腦編譯:

  1. render:function(){ 
  2.     return React.createElement('div',null
  3.         React.createElement('div',{className:'timer'},'定時(shí)器'+this.props.interval), 
  4.         React.createElement('div',null,this.props.title), 
  5.         React.createElement('input',{ref:'input'}), 
  6.         React.createElement('button',null,'click it.'
  7.     ); 

然后將js代碼逆向編譯為coffee。

這里我們可以用$代替React.createElement簡(jiǎn)化代碼(終于可以用jQuery的坑位了),得益于coffee的語法,借助React.DOM可以用一種更簡(jiǎn)單的方式實(shí)現(xiàn):

{div,input,button,span,h1,h2,h3} = React.DOM

這里就不單獨(dú)放render部分,直接看完整代碼:

  1. {Component,PropTypes} = React = require 'react' 
  2. $ = React.createElement 
  3. {div,input,button} = React.DOM 
  4.  
  5. class App extends Component 
  6.     clickHandle:-> 
  7.         dom = this.refs.input.getDOMNode() 
  8.         this.props.actions.change(dom.value) 
  9.         dom.value = '' 
  10.     render:-> 
  11.         {title,interval} = this.props 
  12.         div className:'timer'
  13.             div null,'定時(shí)器:' + interval 
  14.             div null,title 
  15.             input ref:'input' 
  16.             button onClick:@clickHandle.bind(this),'click it.' 
  17.  
  18. App.propTypes = 
  19.     title: PropTypes.string 
  20.     actions: PropTypes.object 
  21.     interval: PropTypes.number 
  22.  
  23. module.exports = App 

如果你能看到并看懂這段coffee,并在大腦里自動(dòng)編譯成js代碼再到j(luò)sx代碼,恭喜你。

連接store

這個(gè)環(huán)節(jié)的作用,主要是實(shí)現(xiàn)view層和store層的綁定,當(dāng)store數(shù)據(jù)變化時(shí),可自動(dòng)更新view。

這里需要使用redux提供的createStore方法創(chuàng)建一個(gè)store,該方法接受2個(gè)參數(shù),reducer和初始的state(應(yīng)用初始數(shù)據(jù))。

store.coffee的代碼如下:

  1. {createStore} = require 'redux' 
  2. reducers = require './reducers' # reducer 
  3. state = require './state' # 應(yīng)用初始數(shù)據(jù) 
  4.  
  5. module.exports = createStore reducers,state 

然后我們?cè)趹?yīng)用的入口將store和App綁定,這里使用了redux官方提供的react-redux庫。

  1. {Provider,connect} = require 'react-redux' 
  2. store = require './store' 
  3. $ = React.createElement 
  4. mapState = (state)-> 
  5.     state 
  6. rootComponent = $ Provider,store:store,-> 
  7.     $ connect(mapState)(App) 
  8. React.render rootComponent,document.body 

可能有人會(huì)問,mapState和Provider是什么鬼?

mapState提供了一個(gè)類似選擇器的效果,當(dāng)一個(gè)應(yīng)用很龐大時(shí),可以選擇將state的某一部分?jǐn)?shù)據(jù)連接到該組件。我們這里用不著,直接返回state自身。

Provider是一個(gè)特殊處理過的react component,官方文檔是這樣描述的:

  1. This makes our store instance available to the components below. 
  2. (Internally, this is done via React undocumented “context” feature,  
  3. but it’s not exposed directly in the API so don’t worry about it.) 

所以,放心的用就好了。

connect方法用于連接state和App,之后即可在App組件內(nèi)部使用this.props.dispatch()方法了。

添加action和reducer

***我們添加一個(gè)按鈕點(diǎn)擊的事件和定時(shí)器,用于觸發(fā)action,并編寫對(duì)應(yīng)的reducer處理數(shù)據(jù)。

在前面的App內(nèi)部已經(jīng)添加了this.props.actions.change(dom.value),這里看下action.coffee的代碼:

  1. module.exports = 
  2.     change:(title)-> 
  3.         type:'change' 
  4.         title: title 
  5.     timer:(interval)-> 
  6.         type:'timer' 
  7.         interval:interval 

再看reducer.coffee

  1. module.exports = (state,action)-> 
  2.     switch action.type 
  3.         when 'change' 
  4.             Object.assign {},state,title:'hello ' + action.title 
  5.         when 'timer' 
  6.             Object.assign {},state,interval:action.interval 
  7.         else 
  8.             state 

至此,代碼寫完了。

一些其他的東西

這里只介紹一個(gè)中間件的思想,其他的特性例如異步action,或者dispatch一個(gè)promise等原理基本類似:

  1. dispatch = store.dispatch 
  2. store.dispatch = (action)-> 
  3.     console.log action # 打印每一次action 
  4.     dispatch.apply store,arguments 


由于時(shí)間關(guān)系,redux的一些特性和設(shè)計(jì)原理沒有展現(xiàn)出來,以后有時(shí)間再單獨(dú)講,完整的項(xiàng)目代碼,感興趣的同學(xué)可以看這里:請(qǐng)點(diǎn)我,或者直接找我聊。

項(xiàng)目用到了fis3作為構(gòu)建工具,使用fis3 release即可在本地查看效果。

 

責(zé)任編輯:王雪燕 來源: IMWeb Team
相關(guān)推薦

2020-06-14 09:04:00

前端云計(jì)算無服務(wù)器

2023-10-27 10:16:17

前端項(xiàng)目Rust

2021-05-07 08:20:52

前端開發(fā)技術(shù)熱點(diǎn)

2016-10-28 15:01:35

Cookie前端實(shí)踐

2021-05-06 05:48:38

RabbitMQ監(jiān)聽前端

2022-11-21 18:02:04

前端測(cè)試

2024-04-22 00:00:00

幽靈依賴前端

2017-10-27 22:03:35

javascrip

2022-03-28 10:25:27

前端文件編譯器

2017-08-07 16:41:39

LinuxOpenCV

2019-01-03 09:45:20

Go 前端 Web

2024-11-27 13:30:00

2017-07-14 14:50:00

Logistic搜索cio

2025-02-04 11:33:19

2011-08-04 15:55:50

Windows 編譯 Objective-

2009-12-16 11:54:35

WindowsLinuxUnix

2017-03-16 18:50:59

2010-01-25 13:43:09

C++算術(shù)編碼

2018-02-24 09:51:03

Linux密碼安全

2020-11-05 10:20:54

前端編碼規(guī)范安全漏洞
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)