-->

Aikau fundamentals - payload 101

2019-07-20 07:28发布

问题:

I am very new to Alfresco Aikau and I just don't get how payload works. There are many examples where payload is used but I am not making the connection.

All the examples I seem to find suggest (to me that is) that the payload is used to define a data list that will be loaded and returned to the widget.

But, the data list always seems to be a url and I assume the result from the url is a json object.

What if I want to call a webscript that build a json object and returns it back to the widget. What would be the mechanism for that? Do I load it into the payload and if so how?

Hopefully you understand my confusion and can point me in the right direction.

Thanks

回答1:

Aikau uses a publication/subscription model to fully decouple all the widgets and services. A "payload" is the object that is published on a topic and consumed by the subscriber. There is no single payload type, each widget and service might be expecting different payloads containing different data. We have been working through all the topics used by widgets out-of-the-box and moving them to a constants module and the JSDoc for this documents the structure of each payload for each topic.

In answer to your specific question - a widget in the lists package (the most basic being alfresco/lists/AlfList) is going to publish a payload requesting the data to be loaded. Lists can work with any service so the topic published on will be different depending upon what service you wish to use, similarly the payload published will be different.

For example, if you're using the CrudService then the payload will need to contain the URL to load. The majority of other services won't require a URL because they are written to work with specific data and use known URLs to retrieve that data.

Although it might be tempting to use the CrudService in all cases, there are many benefits in using dedicated services - including writing your own custom services - in order to normalize the data returned (because many of the core WebScript-based REST APIs don't follow a common schema).

When a service has loaded the data it will publish the data on a response topic - the payload published will contain the actual list data. Once again, the CrudService will have no "knowledge" of the data schema so the list will need to be configured (using the "itemsProperty" attribute) to identify the attribute in the payload that contains the array of data that represents the list to be rendered).

The Aikau tutorial on GitHub works through the process of creating pages that use lists and load list data (from a variety of sources) - I would suggest that you work through the tutorial if you haven't already.

It is also possible to initialise a list with data (via the "currentData" attribute) so that you don't need to make an XHR request for data.