【发布时间】:2017-09-02 06:10:00
【问题描述】:
FullCalendar 有一个名为 Scheduler 的附加组件,我正在尝试将其与 PrimeNG-Schedule 组件一起使用。查看 PrimeNG 文档,我可以使用“选项”属性向 FullCalendar 发送任意信息。这确实有效,但是当我将数据检索连接到异步 API 时,它会导致问题。
API 使用 Observables,然后我在组件中订阅它。这适用于事件,因为当事件更改时视图会自动更新。
但是,当通过 PrimeNG 的“options”属性为 FullCalendar 提供“resources”时,事情不会按预期工作,因为设置“options”属性的代码在 API 调用有机会返回之前运行因此是空的。
我确信这一点,因为如果我对资源进行硬编码,一切都会奏效。
我可以想出几种方法来解决这个问题:
使调用同步(希望避免这种情况)
等待所有数据加载,然后(重新)渲染视图(使其与 #1 几乎相同)
配置 options.resources 属性,这样当它发生变化时,视图会更新,就像它对事件所做的那样(这是最好的选择,但不确定是否可能)
我将不胜感激。谢谢。
<p-schedule
[events]="events"
[businessHours]="businessHours"
[options]="optionConfig"
>
</p-schedule>
我的(目前)虚拟 API
getEvents() {
return this.http
.get('assets/api/mockEvents.json')
.map((response : Response) => <Appointment[]>response.json().data)
.catch(this.handleError);
}
getResources() {
return this.http
.get('assets/api/mockResources.json')
.map((response : Response) => <Resource[]>response.json().data)
.catch(this.handleError);
}
组件文件
ngOnInit() {
this.schedulerService.getEvents()
.subscribe(events=> this.events = events);
this.schedulerService.getResources()
.subscribe(resources => this.resources = resources);
// ***** If the following code is uncommented, resources are displayed in Schedule view ****
// this.resources = [
// new Resource(1, "Dr. Hibbert", "blue", true, new BusinessHours("08:00", "16:00")),
// new Resource(2, "Dr. Simpson", "green", true, new BusinessHours("10:00", "18:00"))
// ];
this.optionConfig = {
"resources": this.resources
}
}
编辑: 我想到的一件事是,仅通过它的 setter 方法设置 this.resources 属性。这样,我可以准确地知道值是在什么时候被设置的,但是问题仍然存在我如何才能将新值推送到调度组件在它已经被初始化之后。
【问题讨论】:
-
在您的
ngOnInit钩子中.subscribe(resources => .resources = resources);行中缺少this,它应该是.subscribe(resources => this.resources = resources); -
@Andriy 哎呀,写帖子时出错了。刚看了我的代码,它确实包含
this -
你能不能在一些 plunker 上重现同样的问题?
标签: angular schedule primeng angular2-observables