您的模式几乎完成了,您只需要避免在引导时创建可观察的,同时,请记住
BehaviorSubject
扩展
Observable
,因此,如果希望使其易于使用,可以原样使用它,使用隐式getter。
@Injectable()
export class TodoService {
private _todos: BehaviorSubject<Todo[]>;
constructor() {
this._todos = new BehaviorSubject<Todo[]>([]);
}
public get todos(): Observable<Todo[]>{
return this._todos;
}
}
然后,当您想要添加数据时,只需在
_todos
主题,请参见
RxJs: Incrementally push stream of data to BehaviorSubject<[]>
对于增量阵列发射,如果希望在每次添加数据时发射所有内容。
编辑
如果服务依赖于
todos
数据,您只需使用
map
,则,
mergeMap
,etc运算符,以建立新的可观察
待办事项
作为源,允许在根数据更改时发出新值。
示例:
@Injectable()
export class ResourcesService{
private _resources: Observable<Resources[]>;
constructor(private todoService: TodoService){
this._resources = this.todoService.todos.map(todos => {
// I suppose you want to aggreagate all resources, you can do it using reduce or anything, I'll use forEach in this example
const resources: Resource[] = [];
todos.forEach(todo => resources.push(...todo.requiredResouces);
});
}
//And then implicit getter for _resources.
}
这样,如果你的
TodoService
发射新阵列,
ResourcesService
将发出状态为“完成”的新TODO数组,而不需要任何其他操作。
另一种方法
如果您的资源来自另一个端点(意味着在更新数据后需要另一个请求来获取它们),那么最好使用重载模式:
@Injectable()
export class DataReloaderService{
public readonly reloader: BehaviorSubject<void> = new BehaviorSubject<void>(null);
public reload():void{
this.reloader.next(null);
}
}
然后,无论何时创建数据服务,只需将其与可观察的重载器合并即可:
@Injectable()
export class ResourceService {
private _resources: Observable<Resource[]>;
constructor(private reloaderService: DataReloaderService, private http: HttpClient){
this._resources = this.reloaderService.reloader.mergeMap(() => this.http.get(...));
}
}
最后,在您的服务中进行修改:
export class TodoService {
private _todos: BehaviorSubject<Todo[]>;
constructor(private reloaderService: DataReloaderService, private http: HttpClient) {
this._todos = this.reloaderService.reloader.mergeMap(() => this.http.get(dataUrl));
}
public get todos(): Observable<Todo[]>{
return this._todos;
}
addResourceRequirement(todo: ToDo, resource: Resource) {
this.http.post(`${url}/${todo.id}/`, {addResource: resource.id})
.subscribe(() => this.reloader.reload());
}
}
注意事项
:您不应该在服务中订阅,Observables的目的是构建冷链,您只需在显示部分订阅,第二种模式确保您的所有Observables都链接到中心重载器(您也可以创建多个重载器,例如每个模型族一个),订阅会使您失去这一点,从而导致只编辑数据的奇怪方式。如果一切都依赖于您的api,那么您的可观察对象应该只使用此api,在您编辑某些内容时调用重载程序,以确保所有链接的数据也都得到更新。