fulcrumapp / fulcrum-expressions

Fulcrum expressions engine
http://developer.fulcrumapp.com/expressions/intro/
5 stars 0 forks source link

Add function to recurse nested fields #22

Open zhm opened 8 years ago

zhm commented 8 years ago

Add a function to handle the common pattern of wanting "all elements under this container" (section or repeatable).

Currently we have:

// all direct child element of the repeatable
FIELD('repeatable').elements

But many, if not most times, it's desirable to have all of the repeatable's descendants.

FIELDS('repeatable');

Possible implementation:

function FIELDS(dataName) {
  var eachRecursive = function(elements, iterator) {
    elements.forEach(function(element) {
      if (element.elements) {
        eachRecursive(element.elements, iterator);
      }

      iterator(element);
    });
  };

  var elements = [];

  eachRecursive(FIELD(dataName), function(element) {
    elements.push(element);
  });

  return elements;
}