apostrophecms / apostrophe

A full-featured, open-source content management framework built with Node.js that empowers organizations by combining in-context editing and headless architecture in a full-stack JS environment.
https://apostrophecms.com
Other
4.36k stars 590 forks source link

As a developer of a custom schema field I can see the current contents of other fields without jQuery hijinx #2061

Closed boutell closed 4 years ago

boutell commented 5 years ago

Proposed method and docs:

apos.schemas.getContextData($el, levels, function(err, data) { ... })

Although related to convert this method should not be used as a substitute. This method is intended as a way for custom schema fields to gain vision into each other's current contents. For example, apostrophe-forms might use this method to determine which form field names should be presented as choices for a conditional display feature.

Given an $el as passed into a schema field's populate() method, a levels of 1 and a callback, it will convert all of the fields present and invoke callback with (err, data).

If levels is 2 complete data for the parent context is converted and delivered. For instance, if this field is a string field nested in an array field, the entire array is passed to the callback as data. object fields are handled similarly.

If levels is 3 the data for the next context up is converted and delivered; if this field is a string nested in an array nested in the schema of a piece, the data will be for the entire piece.

If levels is null or completely omitted from the call, the data for the root context is converted and delivered (the data for the complete piece).

err may contain an array of errors, however data will still contain as much information as possible. For instance, if a required field is missing err will be truthy but there will still be data worth inspecting in data.

If calling getContextData($el, 2, callback) from a field that is nested in an array field, be aware that the array data received may or may not include the particular array item your field is in. If you need to inspect the latest values of your own peer fields use a depth of 1 to get them.

boutell commented 5 years ago

https://github.com/apostrophecms/apostrophe/pull/2062 WIP

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.