英文:
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 './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;
},
},
});
<!-- 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 => column.property === 'customerOrdersCount').length > 0) {
return columns;
}
columns.push({
property: 'customerOrdersCount',
// ...
});
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('getList');
const criteria = new Criteria(this.page, this.limit);
const repository = this.repositoryFactory.create('order_customer');
// ...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论