Extend backend models (Data/DTO) and sync with Spartacus

妖精的绣舞 提交于 2021-01-28 11:44:07

问题


What is the approach if want to add a new attribute to the Product(Model/Data/DTO) in SAP Commerce Cloud and wanna to access it in Spartacus (using Spartacus ProductService)? How to introduce the attribute to the Product model in Spartacus and get it populated with the value from the backend?

This question can be seen as a general question how to apply this requirement to all models and keeping the models in sync between backend and frontend.

Thank you in advance.


回答1:


In order to fetch additional attributes, you can configure the endpoint. See https://sap.github.io/cloud-commerce-spartacus-storefront-docs/connecting-to-other-systems/#configuring-endpoints for more information. There's no need to convert (normalize) the data necessarily, but you could do this as well. This is covered in the same documentation. And you could even replace the standard OCC adapters by a custom adapter if you need to adapt a 3rd party backend.

Once the data is loaded from the backend, it will be stored in the central store, and exposed by the facade without limitation. You might however want to enhance the default typing, in order to benefit from type-safety and not fallback to any. You can do this with the following:

// intro custom typing
interface CustomProduct extends Product {
  customAttribute?: string;
}

// use typing for the observed data
product$: Observable<CustomProduct> = this.currentProductService.getProduct()


来源:https://stackoverflow.com/questions/59966879/extend-backend-models-data-dto-and-sync-with-spartacus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!