简而言之,我在路线的模型钩子中创建了一个记录("索赔人")但是当我试图获得"索赔人时#34;记录在动作处理程序中,Ember表示该记录并不存在。结果,当我试图保存"索赔人时,"恩伯告诉我,它不能阅读财产'保存'未定义的。"我的猜测是我的初始化程序存在问题,但我很难理解为什么。
非常感谢任何帮助。谢谢!
相关代码如下:
模型/ claimant.js
import attr from 'ember-data/attr';
import DS from 'ember-data';
import { belongsTo } from 'ember-data/relationships';
export default DS.Model.extend({
first_name: attr('string'),
submission: DS.belongsTo('submission', {async: false}),
});
模型/ submission.js
import DS from 'ember-data';
import { belongsTo } from 'ember-data/relationships';
export default DS.Model.extend({
claimant: belongsTo('claimant', {async: false}),
});
路由/ claimant.js
import Ember from 'ember';
export default Ember.Route.extend({
actions: {
next: function() {
let claimant = this.get('claimant');
claimant.save().then(claimant => {
this.get('submission').createSubmission({
claimant: claimant
});
});
this.transitionTo('defendant');
},
},
model() {
let claimant = this.get('store').createRecord('claimant', {
first_name: this.get('first_name'),
});
return claimant;
}
});
注意:如果我改为使用let claimant = this.get(' controller.model');记录保存得当,但是"提交"之间的BelongsTo关系。和"索赔人"没有正确设置。不知道为什么。
初始化/提交-initializer.js
import Ember from 'ember';
const {isNone, isEmpty, merge} = Ember;
let submissionProxy = Ember.ObjectProxy.extend({
_currentSubmission: null,
_store:null,
content: function() {
let currentSubmission = this.get('_currentSubmission');
if (isEmpty(currentSubmission)) {
currentSubmission = this.createSubmission();
}
return currentSubmission;
}.property('_currentSubmission'),
current: Ember.computed.alias('content'),
createSubmission(data = {}) {
let newSubmission = this.get('_store').createRecord('submission', data);
this.set('_currentSubmission', newSubmission);
return newSubmission;
},
});
export function initialize(application) {
application.register('submission:proxy', submissionProxy, {singleton: true});
application.inject('submission:proxy', '_store', 'service:store');
application.inject('component', 'submission', 'submission:proxy');
application.inject('route', 'submission', 'submission:proxy');
application.inject('controller', 'submission', 'submission:proxy');
};
export default {
name: 'submission-initializer',
after: ['ember-data'],
initialize: initialize
};
模板/ claimant.hbs
<h2 id="title">Welcome to the Claimant Page!</h2>
<form>
<div class="form-group">
<label for="first_name">First Name</label>
{{input id="first_name" class="form-control" value=model.first_name}}
</div>
<button class="btn btn-primary" {{action 'next' model}}>Next</button>
</form>
答案 0 :(得分:1)
两件事 - A)使用this.get('model')。保存在你的动作中(模型返回了索赔人,但在你的行动中,你无法访问模型中定义的局部变量 - 即'claimant' )
B)在你的模型中,first_name:this.get('first_name')将不起作用 - &gt;您可能希望在路径中定义变量并将其与hbs文件中的输入字段相关联,然后在模型中访问该变量。
希望这有点帮助!