在路线中加载模型时,我无法从localstorage获取关系数据。
我第一次通过ajax请求从服务器获取数据时一切正常,但是一旦我到达新路由并重新加载数据,所有关系都消失了。
我不明白如何重新加载这些关系!理想情况下,我想调用用户检查模型并获取所有hasMany数组。
感谢您的帮助。
这是我的两个模特
从'ember-data'导入DS;
应试question.js:
export default DS.Model.extend({
question : DS.attr('string'),
questionID : DS.attr('string'),
answer1 : DS.attr('string'),
answer2 : DS.attr('string'),
answer3 : DS.attr('string'),
answer4 : DS.attr('string'),
answer5 : DS.attr('string'),
answer6 : DS.attr('string'),
answer7 : DS.attr('string'),
numberOfAnswers : DS.attr('number'),
solutions : DS.attr('string'),
examID : DS.attr('string'),
chosenAnswers : DS.attr('string'),
result : DS.attr('string'),
userexam: DS.belongsTo('user-exam')
});
用户exam.js:
import DS from 'ember-data';
export default DS.Model.extend({
salesforceid : DS.attr('string'),
name : DS.attr('string'),
examType: DS.attr('string'),
resultPercentage : DS.attr('number'),
result : DS.attr('string'),
numberOfQuestions : DS.attr('number'),
rightAnswers : DS.attr('number'),
passingPercentage : DS.attr('string'),
questions: DS.hasMany('exam-question')
});
在我的account.js路线
import Ember from 'ember';
export default Ember.Route.extend({
model: function () {
return this.store.find('user-exam');
},
afterModel: function() {
var applicationController = this.controllerFor('application');
if (!applicationController.isLoggedIn) {
this.transitionTo('index');
}
this.controllerFor('account').send('loadData');
}
});
&安培;控制器(在第一次加载时我设法填充所有数据和关系完美映射)
import Ember from 'ember';
export default Ember.ArrayController.extend({
needs: ['application', 'newexam-modal'],
isLoading: true,
actions:{
loadData: function (){
console.log(this);
this.send('loadExamTypes');
this.send('LoadUserExams');
},
LoadUserExams: function () {
var applicationController = this.get('controllers.application');
var store = this.store;
var accountController = this;
var userexams = store.findAll('user-exam');
accountController.setProperties ({isLoading: true});
userexams.then(function() {
var userProperties = applicationController.getProperties('useremail','currentToken');
var requestdata = '{ "action": "GetExams","useremail":"'+userProperties.useremail+'","secretToken":"'+userProperties.currentToken+'"}';
Ember.$.ajax({
url: "my server url",
type: "POST",
contentType: "application/json",
data: requestdata,
success : function (data) {
//window.console.log(data);
if (userexams.content.get('length') !== data.get('length')){
data.forEach(function (item){
var examname = item.Name;
store.find('user-exam', { name: examname }).then(function(){
},function() {
console.log("items"+item);
store.createRecord('user-exam', {
salesforceid : item.Id,
name : item.Name,
resultPercentage : item.Exam_Result_Percentage__c,
result : item.Exam_Result__c,
numberOfQuestions : item.Number_of_Questions__c,
rightAnswers : item.Right_Answers__c,
passingPercentage : item.Passing_Percentage__c,
examType : item.Exam_Type__r.Name
}).save().then(function (createdexam){
console.log(item.Exam_Questions__r.records);
item.Exam_Questions__r.records.forEach(function (question){
store.createRecord('exam-question', {
question : question.Question__r.Question__c,
questionID : question.Name,
answer1 : question.Question__r.Answer_1__c,
answer2 : question.Question__r.Answer_2__c,
answer3 : question.Question__r.Answer_3__c,
answer4 : question.Question__r.Answer_4__c,
answer5 : question.Question__r.Answer_5__c,
answer6 : question.Question__r.Answer_6__c,
answer7 : question.Question__r.Answer_7__c,
numberOfAnswers : question.Question__r.Number_of_Answers__c,
solutions : question.Question__r.Solutions__c,
examID : question.Exam_Name__c,
chosenAnswers : question.Answer_Chosen__c,
result : question.Result__c,
userexam : createdexam
//store.find('user-exam', {name: item.Name})
}).save();
});
});
});
});
}
accountController.setProperties ({isLoading: false});
},
error : function (data) {
console.log(data);
}
});
});
},
deleteExamData: function() {
console.log('deleting user exams');
this.store.findAll('user-exam').then(function(record){
record.content.forEach(function(rec) {
console.log('deleting exam'+rec);
Ember.run.once(this, function() {
rec.deleteRecord();
rec.save();
});
}, this);
});
}
}
});
在我的帐户模板上,我使用linkto显示用户检查数据。我第一次点击按钮时,数据会加载到模板中,并附带所有相关的子记录。但是当我回到帐户路线并再次点击查看考试时,子记录就会消失
{{#link-to 'exam' this}}view exam »{{/link-to}}
这是我的exam.js路线
import Ember from 'ember';
export default Ember.Route.extend({
examid:'',
model: function(params){
return this.store.find('user-exam', params.exam_id);
},
serialize: function(model){
return {exam_id:model.get('id')};
},
setupController: function(controller, exam) {
controller.set('model', exam);
},
afterModel: function() {
var applicationController = this.controllerFor('application');
if (!applicationController.isLoggedIn) {
this.transitionTo('index');
}
this.controllerFor('exam').send('loadData' );
}
});
my exam.hbs
<div class="jumbotron">
<div class="container">
<h2>This is Exam: {{ name }} !</h2>
</div>
</div>
<div class="container">
{{ questions }}
{{#each question in questions}}
{{question.name}}<br />
{{/each}}
</div>
我的路由器地图:
Router.map(function() {
this.route('register');
this.route('application');
this.route('login');
this.route('account');
this.resource('exam', { path: 'exams/:exam_id' });
this.route('settings');
});
我的本地存储设置是使用ember-cli插件
完成的我的application.js适配器:
import DS from 'ember-data';
export default DS.LSAdapter.extend({
namespace: 'sfdquiz'
});
和序列化器:
import DS from 'ember-data';
export default DS.LSSerializer.extend({
});
答案 0 :(得分:0)
我犯了一个愚蠢的错误! question.name不存在。
{{#each question in questions}}
{{question.name}}<br />
{{/each}}
但我改变了模式,一次加载所有数据使得应用程序太慢。我使用了aftermodel从服务器获取相关对象,并在那时将它们连接到它们的父对象。
model: function(params){
return this.store.find('user-exam', params.exam_id);
},
afterModel: function(exam) {
var applicationController = this.controllerFor('application');
if (!applicationController.isLoggedIn) {
this.transitionTo('index');
} else {
this.controllerFor('exam').send('loadData', exam.get('id'));
}
}