问题描述:

I'm having trouble getting relationship data from localstorage when loading a model in the route.

The first time I get the data from the server through an ajax request everything is fine but as soon as I get to a new route and reload the data all the relationship have disappeard.

I don't understand how to reload these relationship! Ideally I would like to call the user-exam model and get all the hasMany array as well.

thanks for your help.

here are my two models

import DS from 'ember-data';

exam-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')

});

user-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')

});

on my account.js route

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');

}

});

& controller (on first load I manage to populate all data and relationships are mapped perfectly)

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);

});

}

}

});

On my account template I use linkto to display the user-exam data. The first time I click on the button the data loads in template with all associated child records. But When I go back to account route and click on the view exam again the child records disappear

{{#link-to 'exam' this}}view exam »{{/link-to}}

here is my exam.js route

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>

my router map:

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');

});

My localstorage setup is done like so with the ember-cli addon

my application.js adapter:

import DS from 'ember-data';

export default DS.LSAdapter.extend({

namespace: 'sfdquiz'

});

and serializer:

import DS from 'ember-data';

export default DS.LSSerializer.extend({

});

网友答案:

I was making a stupid error! question.name didn't exist.

    {{#each question in questions}}
      {{question.name}}<br />
    {{/each}}

But I changed the pattern loading all the data at once was making the app too slow. I used the aftermodel to go fetch the related objects from the server and connect them to their parent at that moment.

 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'));
        }
 }
相关阅读:
Top