From bfcea9309bf7f5310cb71d80aacfb685308ce207 Mon Sep 17 00:00:00 2001 From: Dave Woestenborghs Date: Mon, 29 May 2017 16:09:23 +0200 Subject: [PATCH] Added ncMediaName filter for name template --- .../NestedContent/Js/nestedcontent.filters.js | 49 ++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/src/Our.Umbraco.NestedContent/Web/UI/App_Plugins/NestedContent/Js/nestedcontent.filters.js b/src/Our.Umbraco.NestedContent/Web/UI/App_Plugins/NestedContent/Js/nestedcontent.filters.js index cecd1fa..b14f76c 100644 --- a/src/Our.Umbraco.NestedContent/Web/UI/App_Plugins/NestedContent/Js/nestedcontent.filters.js +++ b/src/Our.Umbraco.NestedContent/Web/UI/App_Plugins/NestedContent/Js/nestedcontent.filters.js @@ -43,4 +43,51 @@ angular.module("umbraco.filters").filter("ncNodeName", function (editorState, en return ncNodeNameCache.keys[input]; } -}); \ No newline at end of file +}); + +// Filter to take a node id and grab it's name instead +// Usage: {{ pickerAlias | ncMediaName }} + +// Cache for node names so we don't make a ton of requests +var ncMediaNameCache = { + id: "", + keys: {} +} + +angular.module("umbraco.filters").filter("ncMediaName", function (editorState, entityResource) { + + return function (input) { + + // Check we have a value at all + if (input == "" || input.toString() == "0") + return ""; + + var currentNode = editorState.getCurrent(); + + // Ensure a unique cache per editor instance + var key = "ncMediaName_" + currentNode.key; + if (ncMediaNameCache.id != key) { + ncMediaNameCache.id = key; + ncMediaNameCache.keys = {}; + } + + // See if there is a value in the cache and use that + if (ncMediaNameCache.keys[input]) { + return ncMediaNameCache.keys[input]; + } + + // No value, so go fetch one + // We'll put a temp value in the cache though so we don't + // make a load of requests while we wait for a response + ncMediaNameCache.keys[input] = "Loading..."; + + entityResource.getById(input, "Media") + .then(function (ent) { + ncMediaNameCache.keys[input] = ent.name; + }); + + // Return the current value for now + return ncMediaNameCache.keys[input]; + } + +});