Skip to content

Instantly share code, notes, and snippets.

@mattbrailsford
Last active February 13, 2017 15:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattbrailsford/d7f512ca4a6f24ebe70ebed82eb163f5 to your computer and use it in GitHub Desktop.
Save mattbrailsford/d7f512ca4a6f24ebe70ebed82eb163f5 to your computer and use it in GitHub Desktop.
// Filter to take a node id and grab it's name instead
// Usage: {{ pickerAlias | ncNodeName }}
// Cache for node names so we don't make a ton of requests
var ncNodeNameCache = {
id: "",
keys: {}
}
angular.module("umbraco.filters").filter("ncNodeName", function (editorState, entityResource) {
return function(input) {
var currentNode = editorState.getCurrent();
// Ensure a unique cache per editor instance
var key = "ncNodeName_" + currentNode.key;
if (ncNodeNameCache.id != key){
ncNodeNameCache.id = key;
ncNodeNameCache.keys = {};
}
// See if there is a value in the cache and use that
if (ncNodeNameCache.keys[input]){
return ncNodeNameCache.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
ncNodeNameCache.keys[input] = "Loading...";
entityResource.getById(input, "Document")
.then(function(ent) {
ncNodeNameCache.keys[input] = ent.name;
});
// Return the current value for now
return ncNodeNameCache.keys[input];
}
});
@bjarnef
Copy link

bjarnef commented Feb 13, 2017

@mattbrailsford if you add something like this in the beginning, it doesn't stick at "Loading..." if no content node has been selected, but fallback to the default ncNodeName

return function (input) {

        if (input === '') {
            return;
        }

        var currentNode = editorState.getCurrent();
        ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment