Skip to content

Commit

Permalink
Ensure native labels run even when a data type hasn't yet been cached:
Browse files Browse the repository at this point in the history
…kgiszewski#428

Also, more robust handling of the growing array of promises.
  • Loading branch information
Nicholas-Westby committed Sep 13, 2017
1 parent 5e3e37a commit 65fd9e0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 36 deletions.
53 changes: 33 additions & 20 deletions app/services/archetypeCacheService.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
angular.module('umbraco.services').factory('archetypeCacheService', function (archetypePropertyEditorResource) {
angular.module('umbraco.services').factory('archetypeCacheService', function (archetypePropertyEditorResource, $q) {
//private

var isEntityLookupLoading = false;
var entityCache = [];

var isDatatypeLookupLoading = false;
var datatypeCache = [];

return {
getDataTypeFromCache: function(guid) {
return _.find(datatypeCache, function (dt){
return dt.dataTypeGuid == guid;
});
var value = datatypeCache[guid];
if (value) {
return value.value;
}
return null;
},

addDatatypeToCache: function(datatype, dataTypeGuid) {
Expand All @@ -22,31 +23,43 @@ angular.module('umbraco.services').factory('archetypeCacheService', function (ar
datatypeCache.push(datatype);
}
},

getDatatypeByGuid: function(guid) {
var cachedDatatype = this.getDataTypeFromCache(guid);

/**
* Returns information about a data type based on the GUID for the data type.
* @param guid The GUID for the data type.
* @param returnPromise If true, will return the result as a promise rather than as a plain object.
* @returns {*} The data type object, or a promise that will resolve to a data type object.
*/
getDatatypeByGuid: function(guid, returnPromise) {
var cachedDatatype = datatypeCache[guid];

if(cachedDatatype) {
return cachedDatatype;
return returnPromise
? (cachedDatatype.promise || $q.when(cachedDatatype.value))
: cachedDatatype.value;
}

//go get it from server, but this should already be pre-populated from the directive, but I suppose I'll leave this in in case used ad-hoc
if (!isDatatypeLookupLoading) {
isDatatypeLookupLoading = true;
cachedDatatype = {
promise: null,
value: null
};
datatypeCache[guid] = cachedDatatype;

archetypePropertyEditorResource.getDataType(guid).then(function(datatype) {
//go get it from server, but this should already be pre-populated from the directive, but I suppose I'll leave this in in case used ad-hoc
cachedDatatype.promise = archetypePropertyEditorResource.getDataType(guid).then(function(datatype) {

datatype.dataTypeGuid = guid;
datatype.dataTypeGuid = guid;

datatypeCache.push(datatype);
cachedDatatype.promise = null;
cachedDatatype.value = datatype;

isDatatypeLookupLoading = false;
return datatype;
});

return datatype;
});
}
return returnPromise
? cachedDatatype.promise
: null;

return null;
},

getEntityById: function(scope, id, type) {
Expand Down
36 changes: 20 additions & 16 deletions app/services/archetypeLabelService.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ angular.module('umbraco.services').factory('archetypeLabelService', function (ar
*/
function repeatedlyWaitForPromises(promises) {

// Remember the original number of promises being resolved.
var originalLength = promises.length;
return $q.all(promises).then(function () {
// Extract the promises to a local variable, and empty the original array. This will allow
// additional promises to be added to the original array, which will allow for those
// additional promises to be processed after the original promises have been processed.
var originalPromises = promises.splice(0, promises.length);

// Wait for all of the original promises to finish processing.
return $q.all(originalPromises).then(function () {

// If there are new promises, resolve those too.
if (promises.length > originalLength) {
promises = promises.slice(originalLength);
if (promises.length > 0) {
return repeatedlyWaitForPromises(promises);
}

Expand Down Expand Up @@ -491,17 +494,18 @@ angular.module('umbraco.services').factory('archetypeLabelService', function (ar
});

if(propertyConfig) {
var datatype = archetypeCacheService.getDatatypeByGuid(propertyConfig.dataTypeGuid);

if(datatype) {

//try to get built-in label
var label = getNativeLabel(datatype, templateLabelValue, scope);

if(label) {
templateLabelValue = label;
}
}
templateLabelValue = archetypeCacheService.getDatatypeByGuid(propertyConfig.dataTypeGuid, true)
.then(function (datatype) {
if (datatype) {

// Try to get built-in label.
var nativeLabel = getNativeLabel(datatype, rawValue, scope);
return nativeLabel || rawValue;

} else {
return rawValue;
}
});
}

}
Expand Down

0 comments on commit 65fd9e0

Please sign in to comment.