如何将多个模型查询存储到单个状态存储状态管理中

时间:2019-07-18 07:41:39

标签: angular-akita akita

我想创建具有多个查询状态模型的单个商店。 因此,在任何需要的特定组件或服务中,都可以从商店中选择特定的组件。

秋田县国营管理系统有可能做到这一点。

例如:

@Injectable({ providedIn: 'root' })
export class Query1 extends QueryEntity<myState, School> {

  constructor(protected store: myStore) {
    super(store);
  }

}


@Injectable({ providedIn: 'root' })
export class Query2 extends QueryEntity<myState, Employee> {

  constructor(protected store: myStore) {
    super(store);
  }

}

因此,如果我查询特定的Query1或Query2服务,则在组件中获得相应的订阅。

SchoolComponent

var query1 = Quer1.selectAll();

EmployeeComponent

var query2 = Quer2.selectAll();

有人建议我采用正确的方法来达到同样的效果。

1 个答案:

答案 0 :(得分:1)

您可以执行var query1 = Quer1.selectAll({filterBy: (entity:any) =>(entity.staffDept === 'SomeDepartment') && (entity.staffId === someID});,或更合适的方法

@Injectable({
  providedIn: 'root'
})
export class DeliveryQuery extends QueryEntity<DeliveryState, Delivery> {
  selectVisibilityFilter$ = this.select(state => state.ui);
  selectVisibleDeliveries$ = combineLatest(
    this.selectVisibilityFilter$,
    this.selectAll(),
    this.getVisibleDelivery,

  );

  constructor(protected store: DeliveryStore) {
    super(store);
  }



  private getVisibleDelivery(filter, delivery): Delivery[] {
    switch (filter.filter) {
      case 'COMPLETED':
        switch (filter.drivername) {
          case 'All':
            return delivery.filter(d => (d.data.CHECKPOINT_CODE === 'DLC'));
          default:
            return delivery.filter(d => (d.data.CHECKPOINT_CODE === 'DLC') && (d.data.MOBILE_USER_ID === filter.drivername));
        }
      case 'FAILED':
        switch (filter.drivername) {
          case 'All':
            return delivery.filter(d => d.data.CHECKPOINT_CODE === 'DLP');
          default:
            return delivery.filter(d => (d.data.CHECKPOINT_CODE === 'DLP') && (d.data.MOBILE_USER_ID === filter.drivername));
        }

      case 'ASSIGNED':
        switch (filter.drivername) {
          case 'All':
            return delivery.filter(d => d.data.CHECKPOINT_CODE === 'ASSIGNED');
          default:
            return delivery.filter(d => (d.data.CHECKPOINT_CODE === 'ASSIGNED') && (d.data.MOBILE_USER_ID === filter.drivername));
        }

    }
  }
}

然后您将其命名为:this.list = this.deliveryQuery.selectVisibleDeliveries$

我不确定这是否是最好的方法,但这是我在文档中修改方法后得到的。希望它能有所帮助。我也独自使用akita框架:)