The build function

All instances related to the data in the oPB.SummaryLinkStore will have a build function. This includes oPB.SummaryLinkStore, oPB.SummaryLinkStore.SummaryLink and SummaryLinkCollection.

Goal: Provide a relatively easy way to build a string (for instance of HTML) from the SummaryLinkStore data structure.
returns: undefined
arguments: options (required object)
Options needs a property named "builder" of the type "function".

Example code:
var options = {
    builder: function builder(params) {
        var context = this;
        var current = params.current;
        var phase = params.phase;
        var strings = context.strings = context.strings || new Array();
        if (current instanceof oPB.SummaryLinkStore.SummaryLink && phase==="open" || phase==="leaf") {

var result = options.strings.join(';'); //a collection of titles for all the groups and links in the data of instance.

You should always provide a "builder" function to the build function. All the build function does, is iterate over all the data and call the builder function for each instance of data in order. So the builder function you provide may be called many times. For example, if you like to create a HTML representation, you can add a bit of HTML each time the builder function is called. Obviously, you might just as well use it to do something totally different.

The builder function should expect a "params" object with the named properties "current" and "phase". Besides that the "this" value will refer to the options object that was passed on to the build (not the builder) function.

options = {builder:function(params) {
params.current //the instance in the data structure that calls the builder function.
params.phase //one of the following strings: "open", "leaf", "close"
//this === options

For each object that can be a container for more data, the following pattern is used:
options.builder({ current: instance, phase: "open" });
//iterate children, call "build" on them with current options object
options.builder({ current: instance, phase: "close" });

For each object that can not be a container for more data, the following pattern is used:
options.builder({ current: instance, phase: "leaf" }); //this is a link

Important to note is, that "current" may refer to SummaryLinkStore, SummaryLink or SummaryLinkCollection. For the last type, the constructor is private, so to check for this type, you can use oPB.SummaryLinkStore.isSummaryLinkCollection(instance).

Last edited Aug 30, 2015 at 12:14 PM by pbvansplunter, version 5