【发布时间】:2017-03-14 23:10:45
【问题描述】:
简而言之,更新:
我正在寻找类似这样的操作,但要使用 Observable 而不是常规数组:
var i = this.customers.findIndex(customer => customer._id === id);
~i && this.customers[i] = newObject.
我在屏幕上有 2 个组件。左侧是列表组件,右侧是显示组件(想象它是一个 PDF,只是呈现最新的“版本”数据)
当您单击列表中的某个项目时,它会在右侧组件中显示该选定项目的数据。
列表是一个可观察的数组:
items$: Observable<Proposal[]>;
列表中的每个项目都有一个子组件。可以单击单个项目上的图标,这会更改该子项目的数据。孩子有一个事件发射器来告诉父母数据已经改变:
@Output() proposalDataChanged: EventEmitter<string> = new EventEmitter();
父级绑定到它:
<fb-proposal-list-item
[proposal]="proposal" (proposalDataChanged)="handleDataChanged(p)">
</fb-proposal-list-item>
我遇到的问题是,在 handleDataChanged 方法中,我想在 Observable 中搜索已更改的项目,并将其替换为从发射器返回的新有效负载。我不想调用服务器来刷新整个列表。
我需要这样做,以便右侧的组件反映新数据。
我能找到这样的项目:
handleDataChanged(data: Proposal){
this.items$.subscribe((items: Proposal[]) => item = items.find(p => p.id
== data.id));
}
但无法弄清楚如何更新 Observable 中的项目,而不仅仅是找到更改的项目。
我知道我可以通过在其他地方导航然后再次返回以强制刷新来“欺骗”组件,但这也会影响 API(并重新加载页面)。
网址如下所示:
/pages/proposals/manage/-XHzOJY/document
url 中的那个 slug 是当前选中项的 id(在右侧的组件中呈现)。
所以我不能在这里使用参数变化检测,因为它不会改变。用户正在更改已选择的对象,这是可观察数组中的众多对象之一。
更新
这是父组件的完整代码:
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Observable, Subject } from 'rxjs/Rx';
import { Proposal } from '../proposal';
import { ProposalService } from '../proposal.service';
import { SearchService } from '../../services/search-service';
@Component({
selector: 'fb-proposal-list',
templateUrl: './proposal-list.component.html',
styleUrls: ['./proposal-list.component.css']
})
export class ProposalListComponent implements OnInit {
total$: Observable<number>;
items$: Observable<Proposal[]>;
term: string = "";
currentPage: number = 1;
private pageStream = new Subject<number>();
constructor(
private _searchService: SearchService,
private _proposalService: ProposalService,
private _router: Router) {
}
ngOnInit() {
this.setupSearching();
// let timer = Observable.timer(0, 60000);
// timer.subscribe(() => this.goToPage(this.currentPage));
}
setupSearching(){
const searchSource = this._searchService.searchTermStream
.map(searchTerm => {
this.term = searchTerm;
return {search: searchTerm, page: 1}
});
const pageSource = this.pageStream.map(pageNumber => {
this.currentPage = pageNumber;
return {search: this.term, page: pageNumber}
});
const source = pageSource
.merge(searchSource)
.startWith({search: this.term, page: this.currentPage})
.switchMap((params: {search: string, page: number}) => {
return this._proposalService.getProposalsPaged(params.search, params.page)
})
.share();
this.total$ = source.pluck('meta').pluck('total_items');
this.items$ = source.pluck('items');
}
goToPage(page: number) {
this.pageStream.next(page)
}
handleDataChanged(id: string){
this.goToPage(this.currentPage);
}
}
【问题讨论】:
标签: angular rxjs observable