我正在尝试在我的组件中构建一个表单,并且我想将Action从我的组件发送到我的Route。在我的表单中,我有4个输入和一个用于保存的按钮:
template/author.hbs
{{author-profile author=model action="save"}}
template/component/author-profile.hbs
<form class="form-horizontal" {{action "save" on="submit"}}>
{{input type="text" value=author.firstname}}
{{input type="text" value=author.water}}
{{input type="text" value=author.birth.town}}
{{input type="date" value=author.birth.date}}
<button type="submit" class="btn btn-default">Save</button>
</form>
完全对于这种形式我需要2个模型=&gt;作者和出生:
models/author.js
export default DS.Model.extend({
firstname: DS.attr('string'),
surname: DS.attr('string'),
births: DS.hasMany('birth')
});
models/birth.js
export default DS.Model.extend({
author: DS.belongsTo('author'),
date: DS.attr('date'),
town: DS.attr('string')
});
为了在LocalStorage中创建记录,我需要将Action从组件发送到Route:
组件/作者 - profile.js
actions: {
save: function() {
this.sendAction('action');
}
}
现在当我收到Action时,我应该保存但是我收到了这个错误:
Uncaught TypeError: Cannot read property 'get' of null
这是我的路线:
routes/author.js
actions: {
save: function() {
//Create New author
var newAuthor = this.store.createRecord('author', {
firstname: this.get('author.firstname'),
surname: this.get('author.surname'),
town: this.get('author.birth.town'),
date: this.get('author.birth.date')
});
newAuthor.save();
}
}