Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 余烬-初始荷载下的hasMany关系中没有数据_Javascript_Ember.js_Ember Data_Ember Cli - Fatal编程技术网

Javascript 余烬-初始荷载下的hasMany关系中没有数据

Javascript 余烬-初始荷载下的hasMany关系中没有数据,javascript,ember.js,ember-data,ember-cli,Javascript,Ember.js,Ember Data,Ember Cli,余烬cli-3.20,余烬数据-3.30 我试图在控制器设置中修改hasMany关系中的数据,但该关系没有数据。但是,页面完全加载后,所有数据都在那里(即,在我的模板/操作中,所有关系数据都在那里) 我有一个测验应用程序,有许多与问题有关的问题 模型/quick.js import { computed } from '@ember/object'; import DS from 'ember-data'; const { attr, hasMany, Model } = DS; expor

余烬cli-3.20,余烬数据-3.30

我试图在控制器设置中修改hasMany关系中的数据,但该关系没有数据。但是,页面完全加载后,所有数据都在那里(即,在我的模板/操作中,所有关系数据都在那里)

我有一个测验应用程序,有许多与问题有关的问题

模型/quick.js

import { computed } from '@ember/object';
import DS from 'ember-data';

const { attr, hasMany, Model } = DS;

export default Model.extend({
  description: attr('string'),
  questions: hasMany('question', {async: true}) //also tried with async false

});
export default Model.extend({
  question: attr('string'),
  quizzes: hasMany('quiz', {async: true}) //also tried with async false
});
import Route from '@ember/routing/route';

export default Route.extend({
  model(params) { return this.store.findRecord('quiz', params.quiz_id); }
});
import { computed } from '@ember/object';
import Controller from '@ember/controller';

export default Controller.extend({
  quiz: computed.alias('model'),

  //also attempted in setupController/afterModel in router
  modelChanged: function() {
    let quiz = this.get('quiz');
    let questions = quiz.get('questions'); //questions has no data
    questions.then(questions => {
      Promise.all(questions.map(question => {
        //modify questions/answers here
      }));
    });
  }.observes('model')

actions: {
  getQuestions() {
    let questions = this.get('quiz.questions');  //questions now has data
  }
})};
模型/Question.js

import { computed } from '@ember/object';
import DS from 'ember-data';

const { attr, hasMany, Model } = DS;

export default Model.extend({
  description: attr('string'),
  questions: hasMany('question', {async: true}) //also tried with async false

});
export default Model.extend({
  question: attr('string'),
  quizzes: hasMany('quiz', {async: true}) //also tried with async false
});
import Route from '@ember/routing/route';

export default Route.extend({
  model(params) { return this.store.findRecord('quiz', params.quiz_id); }
});
import { computed } from '@ember/object';
import Controller from '@ember/controller';

export default Controller.extend({
  quiz: computed.alias('model'),

  //also attempted in setupController/afterModel in router
  modelChanged: function() {
    let quiz = this.get('quiz');
    let questions = quiz.get('questions'); //questions has no data
    questions.then(questions => {
      Promise.all(questions.map(question => {
        //modify questions/answers here
      }));
    });
  }.observes('model')

actions: {
  getQuestions() {
    let questions = this.get('quiz.questions');  //questions now has data
  }
})};
转到url“/QUITY/1”并在QUITY上路由呼叫findRecord

路线/测验/测验.js

import { computed } from '@ember/object';
import DS from 'ember-data';

const { attr, hasMany, Model } = DS;

export default Model.extend({
  description: attr('string'),
  questions: hasMany('question', {async: true}) //also tried with async false

});
export default Model.extend({
  question: attr('string'),
  quizzes: hasMany('quiz', {async: true}) //also tried with async false
});
import Route from '@ember/routing/route';

export default Route.extend({
  model(params) { return this.store.findRecord('quiz', params.quiz_id); }
});
import { computed } from '@ember/object';
import Controller from '@ember/controller';

export default Controller.extend({
  quiz: computed.alias('model'),

  //also attempted in setupController/afterModel in router
  modelChanged: function() {
    let quiz = this.get('quiz');
    let questions = quiz.get('questions'); //questions has no data
    questions.then(questions => {
      Promise.all(questions.map(question => {
        //modify questions/answers here
      }));
    });
  }.observes('model')

actions: {
  getQuestions() {
    let questions = this.get('quiz.questions');  //questions now has data
  }
})};
controllers/quizzes/quick.js

import { computed } from '@ember/object';
import DS from 'ember-data';

const { attr, hasMany, Model } = DS;

export default Model.extend({
  description: attr('string'),
  questions: hasMany('question', {async: true}) //also tried with async false

});
export default Model.extend({
  question: attr('string'),
  quizzes: hasMany('quiz', {async: true}) //also tried with async false
});
import Route from '@ember/routing/route';

export default Route.extend({
  model(params) { return this.store.findRecord('quiz', params.quiz_id); }
});
import { computed } from '@ember/object';
import Controller from '@ember/controller';

export default Controller.extend({
  quiz: computed.alias('model'),

  //also attempted in setupController/afterModel in router
  modelChanged: function() {
    let quiz = this.get('quiz');
    let questions = quiz.get('questions'); //questions has no data
    questions.then(questions => {
      Promise.all(questions.map(question => {
        //modify questions/answers here
      }));
    });
  }.observes('model')

actions: {
  getQuestions() {
    let questions = this.get('quiz.questions');  //questions now has data
  }
})};
我尝试在setupController()和afterModel()中获取问题数据,但没有成功

注: 测验是嵌套的路线,可以在要显示的每个测验之间进行选择。因此,如果您从“/QUITY/1”导航到“/QUITY/2”,然后返回到“QUITY/1”,问题数据将在观察者、设置控制器、后模型等中可用。因此,第二次访问特定测验时,数据将在设置中可用。(数据始终在模板/操作中可用)

有什么想法吗?

临时解决办法:

在“测验.问题”中使用观察者和旗帜,检查是否第一次击中观察者

import { computed } from '@ember/object';
import Controller from '@ember/controller';

export default Controller.extend({
  quiz: computed.alias('model'),

  areAnswersSet: false,

  observeQuestions: function() {
    let questions = this.get('quiz.questions');
    if (!this.areAnswersSet && questions.length !== 0) {
      this.toggleProperty('areAnswersSet');
      questions.forEach(question => { //modify question });
    }
  }.observes('quiz.questions.[]')

缺点:观察者仍然会被要求回答每一个问题。仅在初始加载时需要。

在Ember Data 3.3.0中有一些与关系相关的错误。值得升级到Ember Data 3.3.1以查看您的问题是否消失…

由于问题关系是异步的,您可能还可以观察
quick.questions.isCompleted
,以便每次加载仅运行一次(可能需要观察As
quick.{questions,questions.isCompleted}
以识别对不同模型的更改。问题)。或者动态添加模型更改的
quick.questions.[]
观察者,并在触发后将其删除。见/。