Mobx计算的吸气剂未更新

时间:2019-10-26 20:17:41

标签: typescript vue.js mobx

我有一个带有两个计算得到的吸气剂的类,但是只有一个正在更新。它们都使用相同的方法,但是一个返回总计数,另一个返回一个数组。

这是只有一个计算函数起作用的类

export class SinglesBatchService {
    @observable private batch: Batch;

     @computed get singlesToPick(): SinglesProduct[] { // not updating
        return this.getProductsWithAmountForStatus([
            OrderStatus.STATUS_PROCESSING,
        ]).singlesProductsByLocation;
    }

    @computed get processedProductsCount(): number { // updating
        return this.getProductsWithAmountForStatus([
            OrderStatus.STATUS_CANCELED,
            OrderStatus.STATUS_PROCESSED,
        ]).productsCount;
    }

    private getProductsWithAmountForStatus(allowedStatuses: OrderStatus[]): SinglesList {
        const singlesList = new SinglesList();

        if (!this.batch) {
            return singlesList;
        }

        this.batch.orders.forEach((order) => {
            if (allowedStatuses.findIndex((a) => a === order.status) === -1) {
                return;
            }

            order.order_rows.forEach((orderRow) => {
                singlesList.addProduct(orderRow.product);
            });
        });

        return singlesList;
    }

}

这是单身人士名单课程

export class SinglesList {
    public singlesProduct: SinglesProduct[];

    constructor() {
       this.singlesProduct = [];
    }

    public addProduct(product: Product): void {
        const index = this.singlesProduct.findIndex((p) => p.product.id === product.id);

        if (index !== -1) {
            this.singlesProduct[index].amount++;
        } else {
            this.singlesProduct.push(new SinglesProduct(1, product));
        }
    }

    public get singlesProductsByLocation(): SinglesProduct[] {
        return this.singlesProduct;
    }

    public get productsCount(): number {
        let count = 0;

        this.singlesProduct.forEach((singleProduct) => {
            count += singleProduct.amount;
        });

        return count;
    }
}

这是单一产品类别

export class SinglesProduct {
    public amount: number;
    public product: Product;

    constructor(amount: number, product: Product) {
        this.amount = amount;
        this.product = product;
    }
}

我在做什么错了?

0 个答案:

没有答案