Administration: 为什么每隔几秒钟就调用 repository.search?

huangapple go评论59阅读模式
英文:

Administration: Why is repository.search called every few seconds?

问题

我想要覆盖订单列表并添加一个新的行,该行进行搜索调用。到目前为止,一切都很顺利,但我不明白为什么 repository.search 每隔几秒钟就会被调用。无论我是将搜索调用放在方法中还是作为计算属性,它总是每隔几秒钟就会被不断调用?!?

import template from './sw-order-list.html.twig';

const { Component } = Shopware;
const { Criteria } = Shopware.Data;

Component.override('sw-order-list', {
    template,

    created() {
        this.repository = this.repositoryFactory.create('order_customer');
    },

    computed: {
        orderColumns() {
            const columns = this.$super('orderColumns');

            columns.push({
                property: 'customerOrdersCount',
                label: 'Test',
                allowResize: true,
                sortable: false,
                align: 'left'
            });

            const criteria = new Criteria(this.page, this.limit);
            const repository = this.repositoryFactory.create('order_customer');

            this.orders.forEach(function (entry) {
                criteria.addFilter(Criteria.equals('customer.id', entry.orderCustomer.customerId));
                const response = repository.search(criteria).then((result) => {                    
                    console.log(response);
                });
            });

            return columns;
        },
    },
});

如果我移除搜索调用,它只会被调用一次。

英文:

I want to override the order listing and add a new row to it which makes a search call. So far so good, but I dont understand why repositiory.search is called every few seconds. It doesnt matter if I put the search call in a method or as computed it is always called after a few seconds over and over?!?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: javascript -->

import template from &#39;./sw-order-list.html.twig&#39;;

const { Component } = Shopware;
const { Criteria } = Shopware.Data;

Component.override(&#39;sw-order-list&#39;, {
    template,

    created() {
        this.repository = this.repositoryFactory.create(&#39;order_customer&#39;);
    },

    computed: {
        orderColumns() {
            const columns = this.$super(&#39;orderColumns&#39;);

            columns.push({
                property: &#39;customerOrdersCount&#39;,
                label: &#39;Test&#39;,
                allowResize: true,
                sortable: false,
                align: &#39;left&#39;
            });

            const criteria = new Criteria(this.page, this.limit);
            const repository = this.repositoryFactory.create(&#39;order_customer&#39;);

            this.orders.forEach(function (entry) {
                criteria.addFilter(Criteria.equals(&#39;customer.id&#39;, entry.orderCustomer.customerId));
                const response = repository.search(criteria).then((result) =&gt; {                    
                    console.log(response);
                });
            });

            return columns;
        },
    },
});

<!-- end snippet -->

If I remove the search call, it is only called once.

答案1

得分: 1

只需考虑一下你在做什么。你有一个计算属性 orderColumns,它依赖于同一属性的超类的状态。你正在向列数组中添加一个新对象,超类的计算属性发生变化,因此触发了你的覆盖计算属性的响应性。这是一个无休止的循环,每次都会执行处理程序,将一个新对象推入列中。

只需检查列是否已包含并提前返回。

if (columns.filter(column => column.property === 'customerOrdersCount').length > 0) {
    return columns;
}

columns.push({
    property: 'customerOrdersCount',
    // ...
});

return columns;

我建议进一步移动处理程序的其余部分,因为它实际上并不依赖于列,并且计算属性不适合使用其响应性行为进行API请求。也许考虑覆盖 getList 方法。

async getList() {
    await this.$super('getList');

    const criteria = new Criteria(this.page, this.limit);
    const repository = this.repositoryFactory.create('order_customer');
    
    // ...
}
英文:

Just think for a second what you're doing here. You have a computed property orderColumns, which is reactive depending on the state of the same property of the superclass. You're pushing a new object to the columns array, the computed property of the superclass changes and as such the reactivity of your override's computed property is triggered. It's an endless loop and you keep executing the handler, pushing a new object into the columns each time.

Simply check if the column is already included and return early.

if (columns.filter(column =&gt; column.property === &#39;customerOrdersCount&#39;).length &gt; 0) {
    return columns;
}

columns.push({
    property: &#39;customerOrdersCount&#39;,
    // ...
});

return columns;

I would further move the rest of the handler, as it is not really depending on the columns and computed properties aren't suited to making API requests with their reactive behavior. Maybe consider overriding the getList method instead.

async getList() {
    await this.$super(&#39;getList&#39;);

    const criteria = new Criteria(this.page, this.limit);
    const repository = this.repositoryFactory.create(&#39;order_customer&#39;);
    
    // ...
}

huangapple
  • 本文由 发表于 2023年6月12日 03:13:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76452140.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定