灰烬:在计算的属性中未检测到模型的属性更改 [英] Ember: Model's property changes not detected in computed property

查看:41
本文介绍了灰烬:在计算的属性中未检测到模型的属性更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个模型。

models / calendar.js

models/calendar.js

export default Model.extend({
  holidays: hasMany('holiday'),
  date: attr(),
  occasion: attr()
})

models / holiday.js

models/holiday.js

export default Model.extend({
  calendar: belongsTo('calendar')
})

和一个组件。

假日/component.js

holidays/component.js

export default Component.extend({
  init() {
    this._super(...arguments);
    this.store.find('calendar', this.get('calendarId'))
      .then(calendar => this.set('calendar', calendar));
  },

  calendarIsDirty: computed('calendar.holidays.@each.{date,occasion}', function () {
    // This is not executed
  })
})

假日/template.hbs

holidays/template.hbs

{{each calendar.holidays as |holiday|}}
  {{input value=holiday.date}}
  {{input value=holiday.occasion}}
{{/each}}

{{#if calendarIsDirty}}
  <button>Save</button>
{{/if}}

我在用户界面中以及何时显示假期用户编辑假期的日期或场合,我想做点事。但是,完全不执行侦听这些更改的计算属性。

I'm displaying the holidays in the UI and when the user edits the date or occasion of the holiday, I want to do something. But the computed property that listens to these changes is not executed at all. Kindly help me with this.

我有一个添加假日按钮,其中添加了一个空的假日,用户可以编辑。当我添加假期时,将执行该计算出的属性。仅当我对现有假期进行更改时,才会执行计算属性。

I have an 'Add Holiday' button that adds an empty holiday that the user can edit. When I add a holiday, that computed property is executed. Only when I make changes to an existing holiday, the computed property is not executed.

推荐答案

您的问题很简单,您假设 this.store.findAll('calendar')返回一个 calendar ,但它返回一个<$ c列表$ c> calendar s。

your problem is simple that you assume that this.store.findAll('calendar') returns a single calendar, but it returns a list of calendars.

因此: this.set('calendar',calendar)您将 calendar 设置为一个 calendar s的数组。

So with this: this.set('calendar', calendar) you set calendar to an array of calendars.

接下来,您的依赖项键 calendar.holidays。@ each 是错误的,因为 calendar 是一个数组,并且因此没有财产假日

Next your dependency key calendar.holidays.@each is wrong because calendar is an array and so has no property holidays.

如果您想要一个日历,您应该执行 findId queryRecord 。如果要使用一组日历,则应相应地更改代码。但是,您需要知道 foo。@ each.bar。@ each 不是有效的依赖项密钥-这是ember的限制。您将需要解决此问题。

If you want a single calendar you should do a findId or queryRecord. If you want an array of calendars you should change your code accordingly. However you need to know that foo.@each.bar.@each is not a valid dependency key - this is a limitation of ember. you would need to work around this.

这篇关于灰烬:在计算的属性中未检测到模型的属性更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆