3

I'm trying to compile the asset model using the reference from this page : https://developer.salesforce.com/docs/atlas.en-us.noversion.mc-apis.meta/mc-apis/compiling-asset-model.htm

I'm trying to understand the code

function compile(asset, channel, mode) {
asset = asset || {};
var content = mode === AssetUtils.COMPILE_MODE.PREVIEW && (asset.superContent || asset.content || asset.design) || asset.content;
if (content) {
    ['slot', 'block'].forEach(function (type) {
        var references = getReferences(content, type);
        var types = type + 's';
        references.forEach(function (reference) {
            var refKey = reference.split('data-key="')[1].split('"')[0];
            if (asset[types] && asset[types][refKey]) {
                content = content.replace(reference, compile(asset[types][refKey]));
            } else {
                console.error('Bad Asset: referenced ' + type + ' does not exist: ' + refKey);
            }
        });
    });
    return content;
} else if (asset.views) {
    if (asset.views[channel]) {
        return compile(asset.views[channel], channel, mode);
    } else if (asset.views.html) {
        return compile(asset.views.html, channel, mode);
    }
}
return '';}

I don't have any js external library reference, so what does it means the AssetUtils.COMPILE_MODE, where can I find this lib ?

Moreover I'm trying to parse a json file, and I don't know what parameters to pass for this function

0

1 Answer 1

2

This is an error in the doc, probably from a copy paste of that come from a larger codebase that uses those COMPILE_MODE variables. Since it compares that value you don't know to the mode argument though, you can replace it with whatever you want or even remove it.

The question here is: do you want to preview the content only (like in a send preview even though AMPScript still won't resolve) or do you also want to have the superContent trump the content, and fallback to design when there is no content (like in the editor preview).

I'm guessing you want it to look like thumbnails in CB, or like the editor. So you can do the following things:

  1. remove the mode from the arguments of the function and the function calls at the end.
  2. assign content to asset.superContent || asset.content || asset.design

Updated code:

function compile(asset, channel) {
asset = asset || {};
var content = asset.superContent || asset.content || asset.design;
if (content) {
    ['slot', 'block'].forEach(function (type) {
        var references = getReferences(content, type);
        var types = type + 's';
        references.forEach(function (reference) {
            var refKey = reference.split('data-key="')[1].split('"')[0];
            if (asset[types] && asset[types][refKey]) {
                content = content.replace(reference, compile(asset[types][refKey]));
            } else {
                console.error('Bad Asset: referenced ' + type + ' does not exist: ' + refKey);
            }
        });
    });
    return content;
} else if (asset.views) {
    if (asset.views[channel]) {
        return compile(asset.views[channel], channel);
    } else if (asset.views.html) {
        return compile(asset.views.html, channel);
    }
}
return '';}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .