-1
使用JavaScript處理大量嵌套括號是很痛苦的。那麼,我怎麼能將這個文件sample.js.jsx
轉換成CoffeeScript並讓它在Ruby on Rails下工作?如何將jsx轉換爲coffeescript並使其與rails兼容
我與寶石react-rails
示例代碼
function getNewSymptomLayer(index) {
return {
index: index,
layer1: "",
layer2: [],
layer3: []
}
}
function SymptonsDataStore() {
this.symptom = [];
this.callback = [];
}
....
var SearchBar = React.createClass({
getInitialState: function() {
return {
data: dataStore.getCurrentState(),
mainSymptomCode: ''
};
},
stateChange: function() {
this.setState({data: dataStore.getCurrentState()});
},
removeRow: function (index, evt) {
this.symptom.splice(index, 1);
this.setState({data: symptoms});
},
render: function() {
var sym = this.state.data.map(function (indexOfSymptoms, index) {
return (<Symptom key={index} data={indexOfSymptoms}/>);
});
dataStore.register(this.stateChange);
var add = function() {
dataStore.addRow();
};
return <div>{sym}
<span onClick={add}>+</span>
</div>;
}
});
React.render(<SearchBar data={dataStore.getCurrentState()}/>, document.getElementById("sym"));
js2coffee不能工作,我認爲它不承認JSX。我會嘗試你提到的下一個 – newBike
對!我已經更新了答案。您需要暫時將您的jsx包裝爲引號,例如: +'轉換爲'「 +」'。轉換和複製粘貼後需要刪除這些引號 – asiniy