Skip to content

Instantly share code, notes, and snippets.

@owenallenaz
Created January 25, 2017 00:25
Show Gist options
  • Save owenallenaz/1b2e341fa560102de94355fbba1097aa to your computer and use it in GitHub Desktop.
Save owenallenaz/1b2e341fa560102de94355fbba1097aa to your computer and use it in GitHub Desktop.
unifying url generation systems
// in api
var buildUrl = miscLib.memoizeSync(function(args) {
return crmLib.buildMultipleDetailUrl({
title : args.title,
recid : args.recid,
defaultPath : plugin._def.settings.detail.path,
detailPath : miscLib.varLookup(plugin._detailTypeIndexBySite, [args.detail_type, args.site]),
sitePath : miscLib.varLookup(site, ["siteConfigs", args.site, "urlNoSlash"]),
absolute : args.absolute
});
});
// in crmLib
var buildMultipleDetailUrl = function(args) {
validator.validate(args, {
type : "object",
schema : [
{ name : "title", type : "string" },
{ name : "recid", type : "number" },
{ name : "absolute", type : "boolean", default : false },
{ name : "defaultPath", type : "string", required : true },
{ name : "detailPath", type : "string" },
{ name : "sitePath", type : "string" },
{ name : "detail_type", type : "string" },
],
required : true,
throwOnInvalid : true,
allowExtraKeys : false
});
if (args.recid === undefined || args.title === undefined) { return; }
var tempPath = args.absolute === true ? args.sitePath : "";
if (tempPath === undefined) { return; } // only way for tempPath to be undefined is if args.absolute === true and args.sitePath is undefined
tempPath += args.detailPath || args.defaultPath;
tempPath += urlLib.slugify(args.title) + "/" + args.recid + "/"
return tempPath;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment