Hi
I’m very new to Ember and Ember-data. I have done some tutorials but now I want to try something on my own. Most tutorials don’t use ember-data and ember-cli though so I have some questions. Specifically about form submission.
How are you supposed to do it? Create an object and bind the attributes to the input fields and have the bindings “synk” the data?
I tried this (I use ember-cli):
// app/models/board.js
import DS from 'ember-data';
export default DS.Model.extend({
owner: DS.attr('string'),
repo_name: DS.attr('string')
});
// app/routes/boards.js
import Ember from 'ember';
export default Ember.Route.extend({
model: function() {
return this.store.findAll('board');
}
});
// app/templates/boards/new.hbs
<div class="row">
<div class="col-md-4 pull-right">
<form {{action "submitBoard" on="submit"}} class="form-inline pull-right">
<div class="form-group">
{{input value=board.owner class="form-control" placeholder="Owner"}}
</div>
<div class="form-group">
{{input value=board.repo_name class="form-control" placeholder="Repo name"}}
</div>
<button class="btn btn-primary" {{action "submitBoard"}}>Create board</button>
</form>
</div>
</div>
<hr>
// app/controllers/boards.js
import Ember from 'ember';
export default Ember.ArrayController.extend({
init: function() {
this.set('board', Ember.Object.create());
},
actions: {
submitBoard: function() {
console.log("Owner:", this.get('board.owner'));
console.log("Repo:", this.get('board.repo_name'));
alert("New Board!");
}
}
});
The boards/new
template is rendered in app/templates/boards.hbs
using {{ render 'boards/new' }}
.
However, this does not work. In the controller, in the submitBoard
action this.get('board.owner')
is undefined.
So, how are you supposed to do it? It seems to be hard to do such a simple thing in Ember so I feel I’m missing something