我試圖使用Ember將組件中的輸入和按鈕組的數據發佈到我的REST API中,但是代碼讓我感到困惑,我還沒有找到一個清楚的例子來說明如何去做。發佈表單數據以從Ember組件中休息api
該網站是一個在守望先鋒的競爭遊戲跟蹤器,所以請在遊戲後輸入排名並在列表中查看。有一個表和API中沒有子項的一個端點。基本上是一個簡單的待辦事項應用程序,但我使用組件來保持一切模塊化。我也想添加編輯和刪除,但這超出了這個問題的範圍。
應用程序/模板/組件/附加game.hbs
<form class="form-inline">
<div class="form-group">
<label class="control-label">
Rank <small>(After match)</small>
{{input value=rank type="number" min="4" class="form-control"}}
</label>
</div>
<div class="form-group">
<label class="control-label">
Outcome
{{#bs-button-group value=outcome type="radio" onChange=(action (mut outcome)) as |bg|}}
{{#bg.button type="default" value='W'}}Win{{/bg.button}}
{{#bg.button type="default" value='D'}}Draw{{/bg.button}}
{{#bg.button type="default" value='L'}}Loss{{/bg.button}}
{{/bs-button-group}}
</label>
</div>
<div class="spacer"></div>
<div class="form-group">
<button {{action 'saveGame'}} type="submit" class="btn btn-default btn-primary">Save</button>
</div>
</form>
應用/組件/附加game.js
import Ember from 'ember';
export default Ember.Component.extend({
actions: {
saveGame() {
const outcome = this.get('model.outcome');
const rank = this.get('model.rank');
console.log(outcome);
console.log(rank);
}
}
});
應用程序/ application.hb
{{nav-bar}}
<div class="container-fluid">
<div class="row">
<div class="col-md-offset-2 col-sm-8 col-md-8 main">
{{add-game}}
<br>
{{game-table model=model.games}}
</div>
</div>
</div>
{{outlet}}
我覺得我在那裏有大部分,我只是缺少一些Ember使用的Data Down,Actions Up方法,但還沒有找到一個清晰的例子來解決這個問題,這與我的代碼很相似。正如你可能會告訴我已將代碼發送到控制檯的代碼一樣,但我不確定將它發送到路由然後發送到服務器的最佳方式。我想我需要使用的模式,但具體代碼
我使用ember-bootstrap的造型和自定義按鈕組組件。
整個項目是Github