-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
72 additions
and
7 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
src/Umbraco.Web.UI.Client/src/common/filters/contentItemResolver.filter.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
(function () { | ||
"use strict"; | ||
|
||
function contentItemResolverFilterService(contentResource, eventsService) { | ||
|
||
var contentKeysRequest = []; | ||
var contentItemCache = []; | ||
|
||
var service = { | ||
|
||
getByKey: function (key) { | ||
// Is it cached, then get that: | ||
const cachedcontentItem = contentItemCache.find(cache => key === cache.key); | ||
if (cachedcontentItem) { | ||
return cachedcontentItem; | ||
} | ||
|
||
// check its not already being loaded, and then start loading: | ||
if (contentKeysRequest.indexOf(key) === -1) { | ||
contentKeysRequest.push(key); | ||
contentResource.getById(key).then(contentItem => { | ||
if (contentItem) { | ||
contentItemCache.push(contentItem); | ||
} | ||
}); | ||
} | ||
|
||
return null; | ||
} | ||
}; | ||
|
||
eventsService.on("content.saved", function (name, args) { | ||
const index = contentItemCache.findIndex(cache => cache.key === args.content.key); | ||
if (index !== -1) { | ||
contentItemCache[index] = args.content; | ||
} | ||
}); | ||
|
||
return service; | ||
|
||
} | ||
|
||
angular.module("umbraco.filters").factory("contentItemResolverFilterService", contentItemResolverFilterService); | ||
|
||
|
||
// Filter loads content Item Model from a content Key. | ||
// Usage: {{ mycontentProperty[0].contentKey | contentItemResolver }} | ||
angular.module("umbraco.filters").filter("contentItemResolver", function (contentItemResolverFilterService) { | ||
|
||
contentItemResolverFilter.$stateful = true; | ||
function contentItemResolverFilter(input) { | ||
|
||
// Check we have a value at all | ||
if (typeof input === 'string' && input.length > 0) { | ||
return contentItemResolverFilterService.getByKey(input); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
return contentItemResolverFilter; | ||
|
||
}); | ||
|
||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters