[英]Because Observable Subscription Happens on the Component, I'm Having Difficulty Refactoring Logic Into Service Layer on Angular 2 App
The one thing I'm finding in using observables in my Angular 2 app is that, because nothing actually "happens" until you subscribe to the observable in the component, that makes it a little tricky to refactor by moving logic into a service layer. 我在Angular 2应用程序中使用可观察对象时发现的一件事是,因为在您订阅了组件中的可观察对象之前,没有任何东西真正“发生”,这使得通过将逻辑移到服务层进行重构变得有些棘手。
For instance, here is an example of a function that receives a sorting parameter, and then filters the data shown to the view accordingly. 例如,这是一个函数的示例,该函数接收排序参数,然后相应地过滤显示给视图的数据。 This uses an observable where subscription is happening in my components, like so:
这在我的组件中发生订阅的地方使用了一个可观察的地方,就像这样:
onSortReceived(sort)
{
if (sort === 'alphabetical')
{
this.filtersService.getByFilter(this.page, this.pagesize, this.body)
.subscribe(resRecordsData => {
this.records = resRecordsData;
this.data = resRecordsData.data;
this.data.sort((a, b) => a.name.last.localeCompare(b.name.last));
},
responseRecordsError => this.errorMsg = responseRecordsError);
}
else if (sort === 'reverse alphabetical')
{
this.filtersService.getByFilter(this.page, this.pagesize, this.body)
.subscribe(resRecordsData => {
this.records = resRecordsData;
this.data = resRecordsData.data;
this.data.sort((a, b) => b.name.last.localeCompare(a.name.last));
},
responseRecordsError => this.errorMsg = responseRecordsError);
}
else if (sort === 'id')
{
this.filtersService.getByFilter(this.page, this.pagesize, this.body)
.subscribe(resRecordsData => {
this.records = resRecordsData;
this.data = resRecordsData.data;
this.data.sort((a, b) => b._id.localeCompare(a._id));
},
responseRecordsError => this.errorMsg = responseRecordsError);
}
else if (sort === 'reset') {
this.filtersService.getByFilter(this.page, this.pagesize, this.body)
.subscribe(resRecordsData => {
this.records = resRecordsData;
this.data = resRecordsData.data;
},
responseRecordsError => this.errorMsg = responseRecordsError);
}
}
Now, rather than have to repeat this code in each component that does this, I'd like to move much of this logic out to a service layer. 现在,我不必在执行此操作的每个组件中重复此代码,而是想将大部分逻辑移到服务层。 But since the filtering is happening AFTER subscription, and subscription happens at the level of the component, I'm not sure how to do this.
但是由于过滤是在订阅之后进行的,并且订阅发生在组件级别,所以我不确定如何执行此操作。 Is there a way I can filter first, and THEN subscribe to the observable?
有没有一种方法我可以先过滤,然后订阅可观察对象? What would that look like?
那会是什么样? If I could do this I could move much of this code out to a service layer.
如果可以这样做,我可以将大部分代码移到服务层。
How about something like this: 这样的事情怎么样:
filter: any;
onSortReceived(sort)
{
if (sort === 'alphabetical')
{
this.filter = (a, b) => a.name.last.localeCompare(b.name.last);
}
else if (sort === 'reverse alphabetical')
{
this.filter = (a, b) => b.name.last.localeCompare(a.name.last);
}
else if (sort === 'id')
{
this.filter = (a, b) => b._id.localeCompare(a._id);
}
else if (sort === 'reset')
{
this.filter = undefined;
}
this.filtersService.getByFilter(this.page, this.pagesize, this.body)
.subscribe(resRecordsData => {
this.records = resRecordsData;
this.data = resRecordsData.data;
this.data.sort(this.filter);
},
responseRecordsError => this.errorMsg = responseRecordsError);
}
Then you could more readily move code into a service as needed. 然后,您可以根据需要更轻松地将代码移入服务。
NOTE: This code was not syntax checked or tested. 注意:此代码未经语法检查或测试。
Taking into account quote 考虑报价
is there a way I can filter first, and THEN subscribe to the observable
有没有一种方法可以先过滤,然后订阅可观察的
.map((response: Response) => response.json().data)
.filter(data => data.Type === 'myfilter')
or like this 或像这样
.map(content => response.json().data)
.concatMap(arr => Observable.from(arr))
.filter(item => item.Type === 'myfilter')
.subscribe(val => console.log(val))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.