重构SPFx Web Apt中的休息请求

时间:2019-04-04 11:46:13

标签: rest spfx

我曾在Web部件中工作过,该Web部件从SP列表中获取项目,然后对这些结果进行过滤或分组。 对于每个过滤器操作,我都会使用Rest向SP发送一个新请求,并且我正在使用rest代码检索过滤出的项目并将其显示在Web部件中。 现在,我有两个由onClick事件触发的过滤器操作。

每个动作如下所示: 要按封闭的协议进行过滤,请执行以下操作:

private getEnded(): void {
this.props.provider.getEnded().then((originalitems: IList[]) => {

  this.setState({
    filteredListItems: originalitems,
    filter: true
  });
});

}

并根据过去的一天进行过滤:

 private getPassed(): void {
this.props.provider.getPassed().then((originalitems: IList[]) => {
  this.setState({
    filteredListItems: originalitems,
    filter: true
  });
});

}

在dataprovider文件中,我有以下方法,它们每个都向SharePoint发出请求,唯一的区别是筛选器参数:

要获取封闭协议(从getEnded()方法中调用):

public async getEnded(): Promise<IList[]> {
let today = new Date();
let Agreements: IList[] = [];

const items = await sp.web.lists
  .getByTitle("AgreementDatabase")
  .items.select(this.select)
  .filter(
    `(AgreementEnded eq true) and (AgreementEndDate le '${today.toISOString()}')`
  )
  .expand("SalesManager,TaxCatchAll,FrameworkAgreement")
  .get();

items.forEach(item => {
  Agreements.push({
    Title: item.Title,
    Id: item.Id,
    CustomerAgreementNr: item.CustomerAgreementNr,
    AgreementType: item.AgreementType,
    Customer: item.TaxCatchAll[0].Term,
    FrameworkAgreement: item.FrameworkAgreement.Title,
    ContactPerson: item.ContactPerson,
    SalesManager: item.SalesManager.FirstName + " " + item.SalesManager.LastName,
    DeliveryType: item.DeliveryType,
    AgreementStartDate: item.AgreementStartDate,
    AgreementEndDate: item.AgreementEndDate,
    AgreementEnded: item.AgreementEnded,
    LastPriceAdjustment: item.LastPriceAdjustment,
    NextPriceAdjustment: item.NexPriceAdjustment,
  });
});

return new Promise<IList[]>(async resolve => {
  resolve(Agreements);
});

}

并通过传递的协议(从方法getPassed()调用):

public async getPassed(): Promise<IList[]> {
let today = new Date();
let Agreements: IList[] = [];


const items = await sp.web.lists
  .getByTitle("AgreementDatabase")
  .items.select(this.select)
  .filter(`LastPriceAdjustment le '${today.toISOString()}'`)
  .expand("SalesManager,TaxCatchAll,FrameworkAgreement")
  .get();

items.forEach(item => {
  Agreements.push({
    Title: item.Title,
    Id: item.Id,
    CustomerAgreementNr: item.CustomerAgreementNr,
    AgreementType: item.AgreementType,
    Customer: item.TaxCatchAll[0].Term,
    FrameworkAgreement: item.FrameworkAgreement.Title,
    ContactPerson: item.ContactPerson,
    SalesManager: item.SalesManager.FirstName + " " + item.SalesManager.LastName,
    DeliveryType: item.DeliveryType,
    AgreementStartDate: item.AgreementStartDate,
    AgreementEndDate: item.AgreementEndDate,
    AgreementEnded: item.AgreementEnded,
    LastPriceAdjustment: item.LastPriceAdjustment,
    NextPriceAdjustment: item.NexPriceAdjustment,
  });
});

return new Promise<IList[]>(async resolve => {
  resolve(Agreements);
});

}

如您所见,从SP请求信息的方法几乎相同,唯一的区别是filter参数。 我想知道如何重新使用其中一种休息方法来通过共享点获取并过滤数据?

最好的问候 美国

1 个答案:

答案 0 :(得分:0)

您可以编写一种方法并传递文件管理器:

A

其他改进:

您可以使用.map()代替.foreach()

private getEnded(): void {
const filter = `(AgreementEnded eq true) and (AgreementEndDate le '${today.toISOString()}')`;
this.props.provider.getAgreements(filter).then((originalitems: IList[]) => {

  this.setState({
    filteredListItems: originalitems,
    filter: true
  });
});



 private getPassed(): void {
const filter = `LastPriceAdjustment le '${today.toISOString()}'`;
    this.props.provider.getAgreements(filter).then((originalitems: IList[]) => {
      this.setState({
        filteredListItems: originalitems,
        filter: true
      });
    });

public async getAgreements(filterAgreements: string): Promise<IList[]> {
let today = new Date();
let Agreements: IList[] = [];

const items = await sp.web.lists
  .getByTitle("AgreementDatabase")
  .items.select(this.select)
  .filter(filterAgreements)
  .expand("SalesManager,TaxCatchAll,FrameworkAgreement")
  .get();

items.forEach(item => {
  Agreements.push({
    Title: item.Title,
    Id: item.Id,
    CustomerAgreementNr: item.CustomerAgreementNr,
    AgreementType: item.AgreementType,
    Customer: item.TaxCatchAll[0].Term,
    FrameworkAgreement: item.FrameworkAgreement.Title,
    ContactPerson: item.ContactPerson,
    SalesManager: item.SalesManager.FirstName + " " + item.SalesManager.LastName,
    DeliveryType: item.DeliveryType,
    AgreementStartDate: item.AgreementStartDate,
    AgreementEndDate: item.AgreementEndDate,
    AgreementEnded: item.AgreementEnded,
    LastPriceAdjustment: item.LastPriceAdjustment,
    NextPriceAdjustment: item.NexPriceAdjustment,
  });
});

return new Promise<IList[]>(async resolve => {
  resolve(Agreements);
});