Update sampleLabelHelpers and inject entityResource instead of off the scope

This commit is contained in:
Kevin Giszewski
2017-09-15 13:39:18 -04:00
parent 123546b11c
commit 03d511ca80
3 changed files with 50 additions and 17 deletions
+37 -4
View File
@@ -1,10 +1,43 @@
function testPromise(value) {
//create a namespace (optional)
var ArchetypeSampleLabelHelpers = {};
//console.log(value);
return function ($timeout) {
//create a function
//you will add it to your label template field as `{{ArchetypeSampleLabelHelpers.testPromise(someArchetypePropertyAlias)}}`
ArchetypeSampleLabelHelpers.testPromise = function(value) {
//you can inject services
return function ($timeout, archetypeCacheService) {
//best to return a promise
//NOTE: $timeout returns a promise
return $timeout(function () {
return "As Promised: " + value;
}, 1000);
}
}
ArchetypeSampleLabelHelpers.testEntityPromise = function(value) {
return function ($q, entityResource) {
var deferred = $q.defer();
entityResource.getById(1054, 'document').then(function(entity) {
console.log("Hello from testEntityPromise");
console.log(entity);
deferred.resolve(entity.name);
});
return deferred.promise;
}
}
ArchetypeSampleLabelHelpers.testEntityPromise2 = function(value) {
return function ($q, archetypeCacheService) {
var deferred = $q.defer();
archetypeCacheService.getEntityById(1054, 'document').then(function(entity) {
console.log("Hello from testEntityPromise2");
console.log(entity);
deferred.resolve(entity.name);
});
return deferred.promise;
}
}
+5 -5
View File
@@ -1,4 +1,4 @@
angular.module('umbraco.services').factory('archetypeCacheService', function (archetypePropertyEditorResource, $q) {
angular.module('umbraco.services').factory('archetypeCacheService', function (archetypePropertyEditorResource, $q, entityResource) {
//private
var entityCache = [];
var datatypeCache = [];
@@ -27,7 +27,7 @@ angular.module('umbraco.services').factory('archetypeCacheService', function (ar
},
//perhaps this should return a promise?
getEntityById: function(scope, id, type) {
getEntityById: function(id, type) {
var deferred = $q.defer();
var cachedEntity = _.find(entityCache, function (e){
@@ -41,7 +41,7 @@ angular.module('umbraco.services').factory('archetypeCacheService', function (ar
}
//go get it from server
scope.resources.entityResource.getById(id, type).then(function(entity) {
entityResource.getById(id, type).then(function(entity) {
entityCache.push(entity);
//console.log("entity is now resolved into cache...");
@@ -53,7 +53,7 @@ angular.module('umbraco.services').factory('archetypeCacheService', function (ar
},
//perhaps this should return a promise?
getEntityByUmbracoId: function(scope, udi, type) {
getEntityByUmbracoId: function(udi, type) {
var deferred = $q.defer();
var cachedEntity = _.find(entityCache, function (e){
@@ -67,7 +67,7 @@ angular.module('umbraco.services').factory('archetypeCacheService', function (ar
}
//go get it from server
scope.resources.entityResource.getByIds([udi], type).then(function (entities) {
entityResource.getByIds([udi], type).then(function (entities) {
// prevent infinite lookups with a default entity
var entity = entities.length > 0 ? entities[0] : { udi: udi, name: "" };
+8 -8
View File
@@ -250,7 +250,7 @@ angular.module('umbraco.services').factory('archetypeLabelService', function (ar
_.each(ids, function(id){
if(id) {
promises.push(archetypeCacheService.getEntityById(scope, id, type).then(function(entity){
promises.push(archetypeCacheService.getEntityById(id, type).then(function(entity){
if(entity) {
entityArray.push(entity.name);
}
@@ -296,7 +296,7 @@ angular.module('umbraco.services').factory('archetypeLabelService', function (ar
_.each(ids, function (id) {
if(id) {
promises.push(archetypeCacheService.getEntityByUmbracoId(scope, id, type).then(function(entity) {
promises.push(archetypeCacheService.getEntityByUmbracoId(id, type).then(function(entity) {
if(entity) {
entityArray.push(entity.name);
}
@@ -323,7 +323,7 @@ angular.module('umbraco.services').factory('archetypeLabelService', function (ar
var deferred = $q.defer();
if(value) {
archetypeCacheService.getEntityById(scope, value, "media").then(function(entity) {
archetypeCacheService.getEntityById(value, "media").then(function(entity) {
deferred.resolve(entity.name);
});
}
@@ -342,7 +342,7 @@ angular.module('umbraco.services').factory('archetypeLabelService', function (ar
//console.log(value);
if(value) {
archetypeCacheService.getEntityByUmbracoId(scope, value, "media").then(function(entity) {
archetypeCacheService.getEntityByUmbracoId(value, "media").then(function(entity) {
deferred.resolve(entity.name);
});
}
@@ -358,7 +358,7 @@ angular.module('umbraco.services').factory('archetypeLabelService', function (ar
var deferred = $q.defer();
if(value) {
archetypeCacheService.getEntityById(scope, value, "document").then(function(entity) {
archetypeCacheService.getEntityById(value, "document").then(function(entity) {
deferred.resolve(entity.name);
});
}
@@ -374,7 +374,7 @@ angular.module('umbraco.services').factory('archetypeLabelService', function (ar
var deferred = $q.defer();
if (value) {
archetypeCacheService.getEntityByUmbracoId(scope, value, "document").then(function(entity) {
archetypeCacheService.getEntityByUmbracoId(value, "document").then(function(entity) {
deferred.resolve(entity.name);
});
}
@@ -413,7 +413,7 @@ angular.module('umbraco.services').factory('archetypeLabelService', function (ar
switch (value.type) {
case "content":
if(value.typeData.contentId) {
archetypeCacheService.getEntityById(scope, value.typeData.contentId, "Document").then(function(entity) {
archetypeCacheService.getEntityById(value.typeData.contentId, "Document").then(function(entity) {
//console.log("Retrived entity from cache!");
//console.log("Resolving the entity name with " + entity[args.propertyName]);
deferred.resolve(entity[args.propertyName]);
@@ -423,7 +423,7 @@ angular.module('umbraco.services').factory('archetypeLabelService', function (ar
case "media":
if(value.typeData.mediaId) {
archetypeCacheService.getEntityById(scope, value.typeData.mediaId, "Media").then(function(entity) {
archetypeCacheService.getEntityById(value.typeData.mediaId, "Media").then(function(entity) {
deferred.resolve(entity[args.propertyName]);
});
}