Created
February 17, 2014 22:16
-
-
Save sbosell/9060337 to your computer and use it in GitHub Desktop.
Umbraco Angular Services
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
angular.module('dw.services', []) | |
.value('serviceBase', "/umbraco/api/pagegenapi/") | |
.factory('serviceApi', ['serviceBase', function (serviceBase) { | |
var service = { | |
base: serviceBase, | |
getMedia: serviceBase + 'GetMedia', | |
getContent: serviceBase + 'getContent', | |
getPath: serviceBase + 'getPage', | |
getChildren: serviceBase + 'getChildren' | |
} | |
return service; | |
}]) | |
.factory('contentService', ['$http', '$q', 'serviceApi', function ($http, $q, serviceApi) { | |
var childCache = {}; | |
var contentCache = {}; | |
var service = { | |
getContentByUrl: function(url){ | |
var d = $q.defer(); | |
var found = ""; | |
for (key in contentCache) | |
{ | |
if (contentCache[key].Url == url) | |
{ | |
found = key; | |
break; | |
} | |
} | |
if (found !=="") | |
{ | |
d.resolve(contentCache[found]); | |
} | |
else { | |
$http({ method: 'GET', headers: { app: true }, url: serviceApi.getPath, params: {url: url} }).success(function (data) | |
{ | |
for (var key in data.Properties) | |
{ | |
data[key] = data.Properties[key].DataValue; | |
} | |
contentCache[data.Id] = data; | |
d.resolve(data); | |
}); | |
} | |
return d.promise; | |
}, | |
getContentById: function (id) { | |
var d = $q.defer(); | |
if (contentCache[id]) | |
{ | |
d.resolve(contentCache[id]); | |
} | |
$http({ method: 'POST', headers: { app: true }, url: serviceApi.getContent }).success(function (data) { | |
for (var key in data.Properties) { | |
data[key] = data.Properties[key].DataValue; | |
} | |
if (!contentCache[id]) | |
contentCache[id] = data; | |
d.resolve(contentCache[id]); | |
}); | |
return d.promise; | |
}, | |
getMediaById: function (mediaId) { | |
var d = $q.defer(); | |
$http({ method: 'POST', data: {id: mediaId}, url: serviceApi.getMedia }).success(function (data) { | |
d.resolve(data); | |
}); | |
return d.promise; | |
}, | |
getChildrenById: function (id) | |
{ | |
var d = $q.defer(); | |
if (!childCache[id]) | |
{ | |
$http({ method: 'POST', data: { id: id }, url: serviceApi.getChildren }).success(function (data) | |
{ | |
childCache[id] = data; | |
d.resolve(childCache[id]); | |
}); | |
} else | |
{ | |
d.resolve(childCache[id]); | |
} | |
return d.promise; | |
} | |
} | |
return service; | |
}]) | |
.factory('templateService', ['$http', '$q', 'serviceBase', function ($http, $q, serviceBase) { | |
var templates = {}; | |
function hasTemplate(templateName) { | |
if (templates[templateName]) | |
return templates[templateName]; | |
return null; | |
}; | |
var service = { | |
test: function () { | |
return "<div></div>"; | |
}, | |
getTemplate: function (url) { | |
// this is the url of the current route | |
if (url == "") url = "/"; | |
var d = $q.defer(); | |
var template = hasTemplate(url) | |
if (!template) { | |
$http({ method: 'GET', url: url, headers: {app: true}}).success(function (data) { | |
d.resolve(data); | |
}); | |
} else { | |
d.resolve(template) | |
} | |
return d.promise; | |
}, | |
getTemplateByAlias: function (alias, url) { | |
// template name from umbraco and url of current route | |
var d = $q.defer(); | |
// check cache first | |
var template = hasTemplate(alias); | |
if (!template) { | |
// why the header app? We are using it on the template to denote it is a call from angular | |
$http({ method: 'GET', url: url, headers: { app: true } }).success(function (data) { | |
templates[alias] = data; | |
d.resolve(templates[alias]); | |
}); | |
} else { | |
d.resolve(template); | |
} | |
return d.promise; | |
} | |
}; | |
return service; | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment