更新服务数据时如何重新呈现Ember.js-Component?

时间:2019-05-19 09:54:15

标签: events ember.js observers

我创建了间隔获取端点的服务。服务获取数据时,我想重新渲染组件。也许我可以创建一些自定义事件或使用观察器来监听已更改的服务?

1 个答案:

答案 0 :(得分:0)

似乎不需要重新渲染组件... 您有一个获取数据的服务,每隔几秒/分钟将数据设置为该服务的某些属性。

// services/fetch.js

fetchDataMethod: .....//method that fetch your data and then set the data to receivedDataProperty

receivedDataProperty: ...//property where fetched data is stored

然后,您可以从应用程序的许多位置访问获取的数据,假设您想添加一些使用获取的数据的统计图,那么您就拥有chart-component

// components/chart-component.js

statisticsData: null

// some code to prepare the statisticsData for some chart library

然后您要在some/route上显示图表

// controllers/some/route.js
....

fetch: service(),

dataForChartComponent: computed('fetch.receivedDataProperty', function() {
  // this computed property will be recalculate every time when  receivedDataProperty was changed
})

然后仅Data Down..到您的chart-component

// templates/some/route.hbs

...
{{chart-component statisticsData=this.dataForChartComponent}}

每次更新服务上的receivedDataProperty时,图表都应更改。