Skip to content

Commit

Permalink
Merge pull request pencilblue#1243 from lgenet/ts-register-for-each
Browse files Browse the repository at this point in the history
Template Service - Register Locals for Each
  • Loading branch information
brianhyder authored Apr 11, 2017
2 parents c5ff27a + b0622ad commit 522def1
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions include/service/entities/template_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,42 @@ module.exports = function(pb) {
return true;
};

/**
* Takes in the below parameters and returns a single TemplateValue that is the combination of
* all items in list, after being ran through the renderFunction.
*
* @param list - The list of data to be used
* @param renderFunction - A function that each item in the list will be ran against
* @param cb - yields error if one occurred, or a single TemplateValue that is the join of
* all values that were returned by the render function
* @returns {*}
*/
TemplateService.prototype.registerLocalForEach = function (list, renderFunction, processFunc, cb) {
// Handle Optional processFunction
if(!cb){
cb = processFunc;
processFunc = function(e) {return e;};
}

if (!list) {
return cb(null, '');
}
else if (!util.isArray(list)) {
list = [list];
}

var tasks = pb.util.getTasks(list, function (item, i) {
return function (callback) {
renderFunction(item[i], callback);
};
});

async.parallel(tasks, function (err, results) {
results = processFunc(results);
cb(err, new pb.TemplateValue(results.join(''), false));
});
};

/**
* Registers a model with the template service. It processes each
* key/value pair in the object and creates a dot notated string
Expand Down

0 comments on commit 522def1

Please sign in to comment.