Angular2 NgRx.存储和提取选择器以供重复使用



这是我的商店:

{
  comments:{
    entities:{id:comment}
    ids:[ids...]
  },
  videos:{
    entities:{id:video}
    ids:[ids...]
  }
}

以下是我的预测:

export function getVideoById(id) {
  return (state$: Observable<AppState>) => _getVideoById(s, id);
}
function _getVideoById(s: AppState, id: string) {
  return s.videos.entities[id];
}
export function getCommentsByVideoId(id) {
  return (state$: Observable<AppState>) =>
    state$.map((s: AppState) => _getCommentsByVideoId(s, id))
}
function _getCommentsByVideoId(s: AppState, id) {
  let currentVid = _getVideoById(s, id);
  return currentVid.commentIds.map(id => s.comments.entities[id]);
}

以下是我如何通过投影访问数据:

this.video$ = this.store.let(getVideoById(this.videoId));
this.comments$ = this.store.let(getCommentsByVideoId(this.videoId));

我的问题:

我目前将我的投影分为两部分,这样我就可以在其他投影(ie: let currentVid = _getVideoById(s, id);)中重用一些部分。

有没有一种方法可以将我的投影声明为一个块,并在其他投影中重用它们?

一种投影构图?

我见过这样的例子,但每次它们都会"缩小"||"缩小"投影的范围(即:我们从视频开始,深入到视频中,我们再也不能回到其他分支,比如"评论"(。,我对这一切都是新手,所以如果能给我一些建议,我将不胜感激

谢谢。

我提出了一种装饰器服务(仍然不确定它是否"正确"(示例:

@Injectable()
export class MyFeatureState {
    // Inject Store to Construtor
   get allFeatureProperties() {
        return this.store.select(s => s.feature.properties).map(toArray)
   }
   getFeaturePropertyById(id:string) {
       return this.store.select(s => s.feature.properties[id])           
   }
   get activeFeatureProperty() {
       return Observable.combineLatest(
           this.allFeatureProperties,
           this.store.select(s => s.feature.activeProperty)
       ).map(([ap, active] => ap[active])
   }
}
interface FeatureState {
    properties:{[id:string]:FeatureProperty};
    activeProperty:string;
}

希望你能得到我的方法:(你也可以利用一些记忆功能来缓存可观测值(它们仍然会发出新的值(

Br

最新更新