Converted all resources over to use a newer structure for generating URLs, created unit tests for those, fixed the js build so that the name is still "umbraco" not "umbraco 7" since that causes problems with css and other file references.
This commit is contained in:
@@ -113,7 +113,7 @@
|
||||
<h1>Using AngularJS Promises and Umbraco Resources</h1>
|
||||
<h2>Promises in Umbraco Resources</h2>
|
||||
<p>All Umbraco resource methods utilize a helper method:</p>
|
||||
<pre><code>angularHelper.resourcePromise</code></pre>
|
||||
<pre><code>umbRequestHelper.resourcePromise</code></pre>
|
||||
<p>This method accepts 2 arguments:</p>
|
||||
<ul>
|
||||
<li>The angular HttpPromise which is created with a call to $http.get (post, etc..)</li>
|
||||
@@ -123,13 +123,13 @@
|
||||
<pre><code>/** Loads in the data to display the nodes menu */
|
||||
loadMenu: function (node) {
|
||||
|
||||
return angularHelper.resourcePromise(
|
||||
return umbRequestHelper.resourcePromise(
|
||||
$http.get(getTreeMenuUrl(node)),
|
||||
"Failed to retreive data for a node's menu " + node.id);
|
||||
}</code></pre>
|
||||
<p>HTTP error handling is performed automatically inside of the <code>angularHelper.resourcePromise</code> and inside of Umbraco's response interceptors.</p>
|
||||
<p>HTTP error handling is performed automatically inside of the <code>umbRequestHelper.resourcePromise</code> and inside of Umbraco's response interceptors.</p>
|
||||
<h2>Consuming Umbraco resources</h2>
|
||||
<p>When consuming Umbraco resources, a normal angular promise will be returned based on the above <code>angularHelper.resourcePromise</code>. The success callback will always receive the RAW json data from the server and the error callback will always receive an object containing these properties:</p>
|
||||
<p>When consuming Umbraco resources, a normal angular promise will be returned based on the above <code>umbRequestHelper.resourcePromise</code>. The success callback will always receive the RAW json data from the server and the error callback will always receive an object containing these properties:</p>
|
||||
<ul>
|
||||
<li>erroMsg = the error message that can be used to show in the UI</li>
|
||||
<li>data = the original data object used to create the promise</li>
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
All Umbraco resource methods utilize a helper method:
|
||||
|
||||
angularHelper.resourcePromise
|
||||
umbRequestHelper.resourcePromise
|
||||
|
||||
This method accepts 2 arguments:
|
||||
|
||||
@@ -16,16 +16,16 @@ Here's an example of the usage in an Umbraco resource. This example is the metho
|
||||
/** Loads in the data to display the nodes menu */
|
||||
loadMenu: function (node) {
|
||||
|
||||
return angularHelper.resourcePromise(
|
||||
return umbRequestHelper.resourcePromise(
|
||||
$http.get(getTreeMenuUrl(node)),
|
||||
"Failed to retreive data for a node's menu " + node.id);
|
||||
}
|
||||
|
||||
HTTP error handling is performed automatically inside of the `angularHelper.resourcePromise` and inside of Umbraco's response interceptors.
|
||||
HTTP error handling is performed automatically inside of the `umbRequestHelper.resourcePromise` and inside of Umbraco's response interceptors.
|
||||
|
||||
##Consuming Umbraco resources
|
||||
|
||||
When consuming Umbraco resources, a normal angular promise will be returned based on the above `angularHelper.resourcePromise`. The success callback will always receive the RAW json data from the server and the error callback will always receive an object containing these properties:
|
||||
When consuming Umbraco resources, a normal angular promise will be returned based on the above `umbRequestHelper.resourcePromise`. The success callback will always receive the RAW json data from the server and the error callback will always receive an object containing these properties:
|
||||
|
||||
* erroMsg = the error message that can be used to show in the UI
|
||||
* data = the original data object used to create the promise
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"author": "Umbraco HQ",
|
||||
"name": "umbraco 7",
|
||||
"name": "umbraco",
|
||||
"homepage": "https://github.com/umbraco/umbraco-cms/tree/7.0.0",
|
||||
"version": "0.0.1-TechnicalPReview",
|
||||
"repository": {
|
||||
|
||||
@@ -1,22 +1,18 @@
|
||||
//TODO: This would be nicer as an angular module so it can be injected into stuff... that'd be heaps nicer, but
|
||||
// how to do that when this is not a regular JS file, it is a server side JS file and RequireJS seems to only want
|
||||
// to force load JS files ?
|
||||
|
||||
//create the namespace (NOTE: This loads before any dependencies so we don't have a namespace mgr so we just create it manually)
|
||||
var Umbraco = {};
|
||||
Umbraco.Sys = {};
|
||||
//define a global static object
|
||||
Umbraco.Sys.ServerVariables = {
|
||||
"umbracoPath": "/umbraco",
|
||||
"contentApiBaseUrl": "/umbraco/UmbracoApi/Content/",
|
||||
"mediaApiBaseUrl": "/umbraco/UmbracoApi/Media/",
|
||||
"sectionApiBaseUrl": "/umbraco/UmbracoApi/Section/",
|
||||
"treeApplicationApiBaseUrl": "/umbraco/UmbracoTrees/ApplicationTreeApi/",
|
||||
"contentTypeApiBaseUrl": "/umbraco/Api/ContentType/",
|
||||
"mediaTypeApiBaseUrl": "/umbraco/Api/MediaTypeApi/",
|
||||
"authenticationApiBaseUrl": "/umbraco/UmbracoApi/Authentication/",
|
||||
|
||||
"MyPackage": {
|
||||
"serverEnvironmentView": "/Belle/PropertyEditors/ServerSidePropertyEditors/ServerEnvironment"
|
||||
umbracoUrls: {
|
||||
"contentApiBaseUrl": "/umbraco/UmbracoApi/Content/",
|
||||
"mediaApiBaseUrl": "/umbraco/UmbracoApi/Media/",
|
||||
"sectionApiBaseUrl": "/umbraco/UmbracoApi/Section/",
|
||||
"treeApplicationApiBaseUrl": "/umbraco/UmbracoTrees/ApplicationTreeApi/",
|
||||
"contentTypeApiBaseUrl": "/umbraco/Api/ContentType/",
|
||||
"mediaTypeApiBaseUrl": "/umbraco/Api/MediaTypeApi/",
|
||||
"authenticationApiBaseUrl": "/umbraco/UmbracoApi/Authentication/"
|
||||
},
|
||||
umbracoSettings: {
|
||||
"umbracoPath": "/umbraco"
|
||||
}
|
||||
};
|
||||
@@ -3,37 +3,32 @@
|
||||
* @name umbraco.resources.authResource
|
||||
* @description Loads in data for authentication
|
||||
**/
|
||||
function authResource($q, $http, angularHelper) {
|
||||
|
||||
/** internal method to get the api url */
|
||||
function getLoginUrl(username, password) {
|
||||
return Umbraco.Sys.ServerVariables.authenticationApiBaseUrl + "PostLogin?username=" + username + "&password=" + password;
|
||||
}
|
||||
|
||||
/** internal method to get the api url */
|
||||
function getIsAuthUrl() {
|
||||
return Umbraco.Sys.ServerVariables.authenticationApiBaseUrl + "GetCurrentUser";
|
||||
}
|
||||
|
||||
//var currentUser;
|
||||
|
||||
function authResource($q, $http, umbRequestHelper) {
|
||||
|
||||
return {
|
||||
//currentUser: currentUser,
|
||||
|
||||
/** Logs the user in if the credentials are good */
|
||||
performLogin: function (username, password) {
|
||||
return angularHelper.resourcePromise(
|
||||
$http.post(getLoginUrl(username, password)),
|
||||
'Login failed for user ' + username);
|
||||
performLogin: function (username, password) {
|
||||
|
||||
return umbRequestHelper.resourcePromise(
|
||||
$http.post(
|
||||
umbRequestHelper.getApiUrl(
|
||||
"authenticationApiBaseUrl",
|
||||
"PostLogin",
|
||||
[{ username: username }, { password: password }])),
|
||||
'Login failed for user ' + username);
|
||||
},
|
||||
|
||||
/** Sends a request to the server to check if the current cookie value is valid for the user */
|
||||
isAuthenticated: function () {
|
||||
|
||||
return angularHelper.resourcePromise(
|
||||
$http.get(getIsAuthUrl()),
|
||||
'Server call failed for checking authorization');
|
||||
return umbRequestHelper.resourcePromise(
|
||||
$http.get(
|
||||
umbRequestHelper.getApiUrl(
|
||||
"authenticationApiBaseUrl",
|
||||
"GetCurrentUser")),
|
||||
'Server call failed for checking authorization');
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -3,52 +3,55 @@
|
||||
* @name umbraco.resources.contentResource
|
||||
* @description Loads/saves in data for content
|
||||
**/
|
||||
function contentResource($q, $http, umbDataFormatter, umbRequestHelper, angularHelper) {
|
||||
function contentResource($q, $http, umbDataFormatter, umbRequestHelper) {
|
||||
|
||||
/** internal method to get the api url */
|
||||
function getContentUrl(contentId) {
|
||||
return Umbraco.Sys.ServerVariables.contentApiBaseUrl + "GetById?id=" + contentId;
|
||||
}
|
||||
/** internal method to get the api url */
|
||||
function getByIdsUrl(ids) {
|
||||
var idQuery = "";
|
||||
_.each(ids, function(item) {
|
||||
idQuery += "ids=" + item + "&";
|
||||
});
|
||||
return Umbraco.Sys.ServerVariables.contentApiBaseUrl + "GetById?" + idQuery;
|
||||
}
|
||||
/** internal method to get the api url */
|
||||
function getEmptyContentUrl(contentTypeAlias, parentId) {
|
||||
return Umbraco.Sys.ServerVariables.contentApiBaseUrl + "GetEmpty?contentTypeAlias=" + contentTypeAlias + "&parentId=" + parentId;
|
||||
}
|
||||
/** internal method to get the api url for publishing */
|
||||
function getSaveUrl() {
|
||||
return Umbraco.Sys.ServerVariables.contentApiBaseUrl + "PostSave";
|
||||
}
|
||||
/** internal method process the saving of data and post processing the result */
|
||||
function saveContentItem(content, action, files) {
|
||||
return umbRequestHelper.postSaveContent(getSaveUrl(content.id), content, action, files);
|
||||
return umbRequestHelper.postSaveContent(
|
||||
umbRequestHelper.getApiUrl(
|
||||
"contentApiBaseUrl",
|
||||
"PostSave"),
|
||||
content, action, files);
|
||||
}
|
||||
|
||||
return {
|
||||
getById: function (id) {
|
||||
return angularHelper.resourcePromise(
|
||||
$http.get(getContentUrl(id)),
|
||||
'Failed to retreive data for content id ' + id);
|
||||
getById: function (id) {
|
||||
return umbRequestHelper.resourcePromise(
|
||||
$http.get(
|
||||
umbRequestHelper.getApiUrl(
|
||||
"contentApiBaseUrl",
|
||||
"GetById",
|
||||
[{ id: id }])),
|
||||
'Failed to retreive data for content id ' + id);
|
||||
},
|
||||
|
||||
getByIds: function (ids) {
|
||||
return angularHelper.resourcePromise(
|
||||
$http.get(getByIdsUrl(ids)),
|
||||
'Failed to retreive data for content ids ' + ids);
|
||||
|
||||
var idQuery = "";
|
||||
_.each(ids, function(item) {
|
||||
idQuery += "ids=" + item + "&";
|
||||
});
|
||||
|
||||
return umbRequestHelper.resourcePromise(
|
||||
$http.get(
|
||||
umbRequestHelper.getApiUrl(
|
||||
"contentApiBaseUrl",
|
||||
"GetByIds",
|
||||
idQuery)),
|
||||
'Failed to retreive data for content id ' + id);
|
||||
},
|
||||
|
||||
/** returns an empty content object which can be persistent on the content service
|
||||
requires the parent id and the alias of the content type to base the scaffold on */
|
||||
getScaffold: function (parentId, alias) {
|
||||
return angularHelper.resourcePromise(
|
||||
$http.get(getEmptyContentUrl(alias, parentId)),
|
||||
'Failed to retreive data for empty content item type ' + alias);
|
||||
|
||||
return umbRequestHelper.resourcePromise(
|
||||
$http.get(
|
||||
umbRequestHelper.getApiUrl(
|
||||
"contentApiBaseUrl",
|
||||
"GetEmpty",
|
||||
[{ contentTypeAlias: contentTypeAlias }, { parentId: parentId }])),
|
||||
'Failed to retreive data for empty content item type ' + alias);
|
||||
},
|
||||
|
||||
getChildren: function (parentId, options) {
|
||||
|
||||
@@ -3,12 +3,7 @@
|
||||
* @name umbraco.resources.contentTypeResource
|
||||
* @description Loads in data for content types
|
||||
**/
|
||||
function contentTypeResource($q, $http, $rootScope, angularHelper) {
|
||||
|
||||
/** internal method to get the api url */
|
||||
function getChildContentTypesUrl(contentId) {
|
||||
return Umbraco.Sys.ServerVariables.contentTypeApiBaseUrl + "GetAllowedChildren?contentId=" + contentId;
|
||||
}
|
||||
function contentTypeResource($q, $http, umbRequestHelper) {
|
||||
|
||||
return {
|
||||
|
||||
@@ -44,9 +39,13 @@ function contentTypeResource($q, $http, $rootScope, angularHelper) {
|
||||
//return all types allowed under given document
|
||||
getAllowedTypes: function (contentId) {
|
||||
|
||||
return angularHelper.resourcePromise(
|
||||
$http.get(getChildContentTypesUrl(contentId)),
|
||||
'Failed to retreive data for content id ' + contentId);
|
||||
return umbRequestHelper.resourcePromise(
|
||||
$http.get(
|
||||
umbRequestHelper.getApiUrl(
|
||||
"contentTypeApiBaseUrl",
|
||||
"GetAllowedChildren",
|
||||
[{ contentId: contentId }])),
|
||||
'Failed to retreive data for content id ' + contentId);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@@ -1,65 +1,65 @@
|
||||
/**
|
||||
* @ngdoc service
|
||||
* @name umbraco.resources.treeResource
|
||||
* @description Loads in data for trees
|
||||
* @name umbraco.resources.mediaResource
|
||||
* @description Loads in data for media
|
||||
**/
|
||||
function mediaResource($q, $http, umbDataFormatter, umbRequestHelper, angularHelper) {
|
||||
|
||||
/** internal method to get the api url */
|
||||
function getMediaUrl(contentId) {
|
||||
return Umbraco.Sys.ServerVariables.mediaApiBaseUrl + "GetById?id=" + contentId;
|
||||
}
|
||||
|
||||
/** internal method to get the api url */
|
||||
function getEmptyMediaUrl(contentTypeAlias, parentId) {
|
||||
return Umbraco.Sys.ServerVariables.mediaApiBaseUrl + "GetEmpty?contentTypeAlias=" + contentTypeAlias + "&parentId=" + parentId;
|
||||
}
|
||||
|
||||
/** internal method to get the api url */
|
||||
function getRootMediaUrl() {
|
||||
return Umbraco.Sys.ServerVariables.mediaApiBaseUrl + "GetRootMedia";
|
||||
}
|
||||
|
||||
/** internal method to get the api url */
|
||||
function getChildrenMediaUrl(parentId) {
|
||||
return Umbraco.Sys.ServerVariables.mediaApiBaseUrl + "GetChildren?parentId=" + parentId;
|
||||
}
|
||||
|
||||
/** internal method to get the api url for publishing */
|
||||
function getSaveUrl() {
|
||||
return Umbraco.Sys.ServerVariables.mediaApiBaseUrl + "PostSave";
|
||||
}
|
||||
function mediaResource($q, $http, umbDataFormatter, umbRequestHelper) {
|
||||
|
||||
/** internal method process the saving of data and post processing the result */
|
||||
function saveMediaItem(content, action, files) {
|
||||
return umbRequestHelper.postSaveContent(getSaveUrl(content.id), content, action, files);
|
||||
return umbRequestHelper.postSaveContent(
|
||||
umbRequestHelper.getApiUrl(
|
||||
"mediaApiBaseUrl",
|
||||
"PostSave"),
|
||||
content, action, files);
|
||||
}
|
||||
|
||||
return {
|
||||
getById: function (id) {
|
||||
return angularHelper.resourcePromise(
|
||||
$http.get(getMediaUrl(id)),
|
||||
'Failed to retreive data for media id ' + id);
|
||||
|
||||
return umbRequestHelper.resourcePromise(
|
||||
$http.get(
|
||||
umbRequestHelper.getApiUrl(
|
||||
"mediaApiBaseUrl",
|
||||
"GetById",
|
||||
[{ id: id }])),
|
||||
'Failed to retreive data for media id ' + id);
|
||||
},
|
||||
|
||||
/** returns an empty content object which can be persistent on the content service
|
||||
requires the parent id and the alias of the content type to base the scaffold on */
|
||||
getScaffold: function (parentId, alias) {
|
||||
return angularHelper.resourcePromise(
|
||||
$http.get(getEmptyMediaUrl(alias, parentId)),
|
||||
'Failed to retreive data for empty content item type ' + alias);
|
||||
|
||||
return umbRequestHelper.resourcePromise(
|
||||
$http.get(
|
||||
umbRequestHelper.getApiUrl(
|
||||
"mediaApiBaseUrl",
|
||||
"GetEmpty",
|
||||
[{ contentTypeAlias: contentTypeAlias }, { parentId: parentId }])),
|
||||
'Failed to retreive data for empty media item type ' + alias);
|
||||
|
||||
},
|
||||
|
||||
rootMedia: function () {
|
||||
return angularHelper.resourcePromise(
|
||||
$http.get(getRootMediaUrl()),
|
||||
'Failed to retreive data for application tree ' + section);
|
||||
|
||||
return umbRequestHelper.resourcePromise(
|
||||
$http.get(
|
||||
umbRequestHelper.getApiUrl(
|
||||
"mediaApiBaseUrl",
|
||||
"GetRootMedia")),
|
||||
'Failed to retreive data for root media');
|
||||
|
||||
},
|
||||
|
||||
getChildren: function (parentId) {
|
||||
return angularHelper.resourcePromise(
|
||||
$http.get(getChildrenMediaUrl(parentId)),
|
||||
'Failed to retreive data for application tree ' + section);
|
||||
|
||||
return umbRequestHelper.resourcePromise(
|
||||
$http.get(
|
||||
umbRequestHelper.getApiUrl(
|
||||
"mediaApiBaseUrl",
|
||||
"GetChildren",
|
||||
[{ parentId: parentId }])),
|
||||
'Failed to retreive data for root media');
|
||||
},
|
||||
|
||||
/** saves or updates a media object */
|
||||
|
||||
@@ -1,24 +1,22 @@
|
||||
/**
|
||||
* @ngdoc service
|
||||
* @name umbraco.resources.mediaTypeResource
|
||||
* @description Loads in data for content types
|
||||
* @description Loads in data for media types
|
||||
**/
|
||||
function mediaTypeResource($q, $http, angularHelper) {
|
||||
|
||||
/** internal method to get the api url */
|
||||
function getChildContentTypesUrl(contentId) {
|
||||
return Umbraco.Sys.ServerVariables.mediaTypeApiBaseUrl + "GetAllowedChildren?contentId=" + contentId;
|
||||
}
|
||||
function mediaTypeResource($q, $http, umbRequestHelper) {
|
||||
|
||||
return {
|
||||
|
||||
//return all types allowed under given document
|
||||
getAllowedTypes: function (contentId) {
|
||||
|
||||
return angularHelper.resourcePromise(
|
||||
$http.get(getChildContentTypesUrl(contentId)),
|
||||
'Failed to retreive data for media id ' + contentId);
|
||||
|
||||
return umbRequestHelper.resourcePromise(
|
||||
$http.get(
|
||||
umbRequestHelper.getApiUrl(
|
||||
"mediaTypeApiBaseUrl",
|
||||
"GetAllowedChildren",
|
||||
[{ contentId: contentId }])),
|
||||
'Failed to retreive data for media id ' + contentId);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
/**
|
||||
* @ngdoc service
|
||||
* @name umbraco.resources.section
|
||||
* @name umbraco.resources.sectionResource
|
||||
* @description Loads in data for section
|
||||
**/
|
||||
function sectionResource($q, $http, angularHelper) {
|
||||
function sectionResource($q, $http, umbRequestHelper) {
|
||||
|
||||
/** internal method to get the tree app url */
|
||||
function getSectionsUrl(section) {
|
||||
@@ -15,9 +15,13 @@ function sectionResource($q, $http, angularHelper) {
|
||||
/** Loads in the data to display the section list */
|
||||
getSections: function () {
|
||||
|
||||
return angularHelper.resourcePromise(
|
||||
$http.get(getSectionsUrl()),
|
||||
'Failed to retreive data for sections');
|
||||
return umbRequestHelper.resourcePromise(
|
||||
$http.get(
|
||||
umbRequestHelper.getApiUrl(
|
||||
"sectionApiBaseUrl",
|
||||
"GetSections")),
|
||||
'Failed to retreive data for sections');
|
||||
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -3,12 +3,7 @@
|
||||
* @name umbraco.resources.treeResource
|
||||
* @description Loads in data for trees
|
||||
**/
|
||||
function treeResource($q, $http, angularHelper) {
|
||||
|
||||
/** internal method to get the tree app url */
|
||||
function getTreeAppUrl(section) {
|
||||
return Umbraco.Sys.ServerVariables.treeApplicationApiBaseUrl + "GetApplicationTrees?application=" + section;
|
||||
}
|
||||
function treeResource($q, $http, umbRequestHelper) {
|
||||
|
||||
/** internal method to get the tree node's children url */
|
||||
function getTreeNodesUrl(node) {
|
||||
@@ -31,8 +26,8 @@ function treeResource($q, $http, angularHelper) {
|
||||
|
||||
/** Loads in the data to display the nodes menu */
|
||||
loadMenu: function (node) {
|
||||
|
||||
return angularHelper.resourcePromise(
|
||||
|
||||
return umbRequestHelper.resourcePromise(
|
||||
$http.get(getTreeMenuUrl(node)),
|
||||
"Failed to retreive data for a node's menu " + node.id);
|
||||
},
|
||||
@@ -44,8 +39,12 @@ function treeResource($q, $http, angularHelper) {
|
||||
throw "The object specified for does not contain a 'section' property";
|
||||
}
|
||||
|
||||
return angularHelper.resourcePromise(
|
||||
$http.get(getTreeAppUrl(options.section)),
|
||||
return umbRequestHelper.resourcePromise(
|
||||
$http.get(
|
||||
umbRequestHelper.getApiUrl(
|
||||
"treeApplicationApiBaseUrl",
|
||||
"GetApplicationTrees",
|
||||
[{ application: options.section }])),
|
||||
'Failed to retreive data for application tree ' + options.section);
|
||||
},
|
||||
|
||||
@@ -56,7 +55,7 @@ function treeResource($q, $http, angularHelper) {
|
||||
throw "The options parameter object does not contain the required properties: 'node' and 'section'";
|
||||
}
|
||||
|
||||
return angularHelper.resourcePromise(
|
||||
return umbRequestHelper.resourcePromise(
|
||||
$http.get(getTreeNodesUrl(options.node)),
|
||||
'Failed to retreive data for child nodes ' + options.node.nodeId);
|
||||
}
|
||||
|
||||
@@ -162,8 +162,8 @@ angular.module('umbraco.services')
|
||||
self.ui.currentNode = args.node;
|
||||
|
||||
//ensure the current dialog is cleared before creating another!
|
||||
if (this.ui.currentDialog) {
|
||||
dialogService.close(this.ui.currentDialog);
|
||||
if (self.ui.currentDialog) {
|
||||
dialogService.close(self.ui.currentDialog);
|
||||
}
|
||||
|
||||
var dialog = self.showDialog({
|
||||
|
||||
@@ -32,79 +32,6 @@ angular.module('umbraco.services').factory('legacyJsLoader', legacyJsLoader);
|
||||
function angularHelper($log, $q) {
|
||||
return {
|
||||
|
||||
/**
|
||||
* @ngdoc function
|
||||
* @name resourcePromise
|
||||
* @methodOf umbraco.services.angularHelper
|
||||
* @function
|
||||
*
|
||||
* @description
|
||||
* This returns a promise with an underlying http call, it is a helper method to reduce
|
||||
* the amount of duplicate code needed to query http resources and automatically handle any
|
||||
* Http errors. See /docs/source/using-promises-resources.md
|
||||
*
|
||||
* @param {object} opts A mixed object which can either be a string representing the error message to be
|
||||
* returned OR an object containing either:
|
||||
* { success: successCallback, errorMsg: errorMessage }
|
||||
* OR
|
||||
* { success: successCallback, error: errorCallback }
|
||||
* In both of the above, the successCallback must accept these parameters: data, status, headers, config
|
||||
* If using the errorCallback it must accept these parameters: data, status, headers, config
|
||||
* The success callback must return the data which will be resolved by the deferred object.
|
||||
* The error callback must return an object containing: {errorMsg: errorMessage, data: originalData }
|
||||
*/
|
||||
resourcePromise: function (httpPromise, opts) {
|
||||
var deferred = $q.defer();
|
||||
|
||||
/** The default success callback used if one is not supplied in the opts */
|
||||
function defaultSuccess(data, status, headers, config) {
|
||||
//when it's successful, just return the data
|
||||
return data;
|
||||
}
|
||||
|
||||
/** The default error callback used if one is not supplied in the opts */
|
||||
function defaultError(data, status, headers, config) {
|
||||
return {
|
||||
//NOTE: the default error message here should never be used based on the above docs!
|
||||
errorMsg: (angular.isString(opts) ? opts : 'An error occurred!'),
|
||||
data: data
|
||||
};
|
||||
}
|
||||
|
||||
//create the callbacs based on whats been passed in.
|
||||
var callbacks = {
|
||||
success: (!opts.success ? defaultSuccess : opts.success),
|
||||
error: (!opts.error ? defaultError : opts.error)
|
||||
};
|
||||
|
||||
httpPromise.success(function (data, status, headers, config) {
|
||||
|
||||
//invoke the callback
|
||||
var result = callbacks.success.apply(this, [data, status, headers, config]);
|
||||
|
||||
//when it's successful, just return the data
|
||||
deferred.resolve(result);
|
||||
|
||||
}).error(function(data, status, headers, config) {
|
||||
|
||||
//invoke the callback
|
||||
var result = callbacks.error.apply(this, [data, status, headers, config]);
|
||||
|
||||
//when there's an erorr...
|
||||
// TODO: Deal with the error in a global way
|
||||
|
||||
//return an error object including the error message for UI
|
||||
deferred.reject({
|
||||
errorMsg: result.errorMsg,
|
||||
data: result.data
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
return deferred.promise;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* @ngdoc function
|
||||
* @name safeApply
|
||||
@@ -333,6 +260,140 @@ angular.module('umbraco.services').factory('umbImageHelper', umbImageHelper);
|
||||
function umbRequestHelper($http, $q, umbDataFormatter, angularHelper) {
|
||||
return {
|
||||
|
||||
/**
|
||||
* @ngdoc method
|
||||
* @name umbRequestHelper#dictionaryToQueryString
|
||||
* @methodOf umbRequestHelper
|
||||
* @function
|
||||
*
|
||||
* @description
|
||||
* This will turn an array of key/value pairs into a query string
|
||||
*
|
||||
* @param {Array} queryStrings An array of key/value pairs
|
||||
*/
|
||||
dictionaryToQueryString: function(queryStrings) {
|
||||
|
||||
if (!angular.isArray(queryStrings)) {
|
||||
throw "The queryString parameter is not an array of key value pairs";
|
||||
}
|
||||
|
||||
return _.map(queryStrings, function (item) {
|
||||
var key = null;
|
||||
var val = null;
|
||||
for (var k in item) {
|
||||
key = k;
|
||||
val = item[k];
|
||||
break;
|
||||
}
|
||||
if (key == null || val == null) {
|
||||
throw "The object in the array was not formatted as a key/value pair";
|
||||
}
|
||||
return key + "=" + val;
|
||||
}).join("&");
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* @ngdoc method
|
||||
* @name umbRequestHelper#getApiUrl
|
||||
* @methodOf umbRequestHelper
|
||||
* @function
|
||||
*
|
||||
* @description
|
||||
* This will return the webapi Url for the requested key based on the servervariables collection
|
||||
*
|
||||
* @param {string} apiName The webapi name that is found in the servervariables["umbracoUrls"] dictionary
|
||||
* @param {string} actionName The webapi action name
|
||||
* @param {object} queryStrings Can be either a string or an array containing key/value pairs
|
||||
*/
|
||||
getApiUrl: function(apiName, actionName, queryStrings) {
|
||||
if (!Umbraco || !Umbraco.Sys || !Umbraco.Sys.ServerVariables || !Umbraco.Sys.ServerVariables["umbracoUrls"]) {
|
||||
throw "No server variables defined!";
|
||||
}
|
||||
|
||||
if (!Umbraco.Sys.ServerVariables["umbracoUrls"][apiName]) {
|
||||
throw "No url found for api name " + apiName;
|
||||
}
|
||||
|
||||
return Umbraco.Sys.ServerVariables["umbracoUrls"][apiName] + actionName +
|
||||
(!queryStrings ? "" : "?" + (angular.isString(queryStrings) ? queryStrings : this.dictionaryToQueryString(queryStrings)));
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* @ngdoc function
|
||||
* @name resourcePromise
|
||||
* @methodOf umbraco.services.angularHelper
|
||||
* @function
|
||||
*
|
||||
* @description
|
||||
* This returns a promise with an underlying http call, it is a helper method to reduce
|
||||
* the amount of duplicate code needed to query http resources and automatically handle any
|
||||
* Http errors. See /docs/source/using-promises-resources.md
|
||||
*
|
||||
* @param {object} opts A mixed object which can either be a string representing the error message to be
|
||||
* returned OR an object containing either:
|
||||
* { success: successCallback, errorMsg: errorMessage }
|
||||
* OR
|
||||
* { success: successCallback, error: errorCallback }
|
||||
* In both of the above, the successCallback must accept these parameters: data, status, headers, config
|
||||
* If using the errorCallback it must accept these parameters: data, status, headers, config
|
||||
* The success callback must return the data which will be resolved by the deferred object.
|
||||
* The error callback must return an object containing: {errorMsg: errorMessage, data: originalData }
|
||||
*/
|
||||
resourcePromise: function (httpPromise, opts) {
|
||||
var deferred = $q.defer();
|
||||
|
||||
/** The default success callback used if one is not supplied in the opts */
|
||||
function defaultSuccess(data, status, headers, config) {
|
||||
//when it's successful, just return the data
|
||||
return data;
|
||||
}
|
||||
|
||||
/** The default error callback used if one is not supplied in the opts */
|
||||
function defaultError(data, status, headers, config) {
|
||||
return {
|
||||
//NOTE: the default error message here should never be used based on the above docs!
|
||||
errorMsg: (angular.isString(opts) ? opts : 'An error occurred!'),
|
||||
data: data
|
||||
};
|
||||
}
|
||||
|
||||
//create the callbacs based on whats been passed in.
|
||||
var callbacks = {
|
||||
success: (!opts.success ? defaultSuccess : opts.success),
|
||||
error: (!opts.error ? defaultError : opts.error)
|
||||
};
|
||||
|
||||
httpPromise.success(function (data, status, headers, config) {
|
||||
|
||||
//invoke the callback
|
||||
var result = callbacks.success.apply(this, [data, status, headers, config]);
|
||||
|
||||
//when it's successful, just return the data
|
||||
deferred.resolve(result);
|
||||
|
||||
}).error(function (data, status, headers, config) {
|
||||
|
||||
//invoke the callback
|
||||
var result = callbacks.error.apply(this, [data, status, headers, config]);
|
||||
|
||||
//when there's an erorr...
|
||||
// TODO: Deal with the error in a global way
|
||||
|
||||
//return an error object including the error message for UI
|
||||
deferred.reject({
|
||||
errorMsg: result.errorMsg,
|
||||
data: result.data
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
return deferred.promise;
|
||||
|
||||
},
|
||||
|
||||
/** Used for saving media/content specifically */
|
||||
postSaveContent: function (restApiUrl, content, action, files) {
|
||||
|
||||
var deferred = $q.defer();
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
describe('umbRequestHelper tests', function () {
|
||||
var umbRequestHelper;
|
||||
|
||||
beforeEach(module('umbraco.services'));
|
||||
|
||||
beforeEach(inject(function ($injector) {
|
||||
umbRequestHelper = $injector.get('umbRequestHelper');
|
||||
}));
|
||||
|
||||
describe('formatting Urls', function () {
|
||||
|
||||
it('can create a query string from name value pairs', function () {
|
||||
|
||||
expect(umbRequestHelper.dictionaryToQueryString(
|
||||
[{ key1: "value1" }, { key2: "value2" }, { key3: "value3" }])).toBe(
|
||||
"key1=value1&key2=value2&key3=value3");
|
||||
});
|
||||
|
||||
it('can create a url based on server vars', function () {
|
||||
|
||||
expect(umbRequestHelper.getApiUrl("contentTypeApiBaseUrl", "GetAllowedChildren", [{contentId: 123}])).toBe(
|
||||
"/umbraco/Api/ContentType/GetAllowedChildren?contentId=123");
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
@@ -48,15 +48,26 @@ namespace Umbraco.Web.Editors
|
||||
//now we need to build up the variables
|
||||
var d = new Dictionary<string, object>
|
||||
{
|
||||
{"umbracoPath", GlobalSettings.Path},
|
||||
{"legacyTreeJs", Url.Action("LegacyTreeJs", "BackOffice")},
|
||||
{"contentApiBaseUrl", Url.GetUmbracoApiService<ContentController>("PostSave").TrimEnd("PostSave")},
|
||||
{"mediaApiBaseUrl", Url.GetUmbracoApiService<MediaController>("GetRootMedia").TrimEnd("GetRootMedia")},
|
||||
{"sectionApiBaseUrl", Url.GetUmbracoApiService<SectionController>("GetSections").TrimEnd("GetSections")},
|
||||
{"treeApplicationApiBaseUrl", Url.GetUmbracoApiService<ApplicationTreeController>("GetApplicationTrees").TrimEnd("GetApplicationTrees")},
|
||||
{"contentTypeApiBaseUrl", Url.GetUmbracoApiService<ContentTypeController>("GetAllowedChildren").TrimEnd("GetAllowedChildren")},
|
||||
{"mediaTypeApiBaseUrl", Url.GetUmbracoApiService<MediaTypeApiController>("GetAllowedChildren").TrimEnd("GetAllowedChildren")},
|
||||
{"authenticationApiBaseUrl", Url.GetUmbracoApiService<AuthenticationController>("PostLogin").TrimEnd("PostLogin")}
|
||||
{
|
||||
"umbracoUrls", new Dictionary<string, object>
|
||||
{
|
||||
{"legacyTreeJs", Url.Action("LegacyTreeJs", "BackOffice")},
|
||||
//API URLs
|
||||
{"contentApiBaseUrl", Url.GetUmbracoApiServiceBaseUrl<ContentController>("PostSave")},
|
||||
{"mediaApiBaseUrl", Url.GetUmbracoApiServiceBaseUrl<MediaController>("GetRootMedia")},
|
||||
{"sectionApiBaseUrl", Url.GetUmbracoApiServiceBaseUrl<SectionController>("GetSections")},
|
||||
{"treeApplicationApiBaseUrl", Url.GetUmbracoApiServiceBaseUrl<ApplicationTreeController>("GetApplicationTrees")},
|
||||
{"contentTypeApiBaseUrl", Url.GetUmbracoApiServiceBaseUrl<ContentTypeController>("GetAllowedChildren")},
|
||||
{"mediaTypeApiBaseUrl", Url.GetUmbracoApiServiceBaseUrl<MediaTypeApiController>("GetAllowedChildren")},
|
||||
{"authenticationApiBaseUrl", Url.GetUmbracoApiServiceBaseUrl<AuthenticationController>("PostLogin")}
|
||||
}
|
||||
},
|
||||
{
|
||||
"umbracoSettings", new Dictionary<string, object>
|
||||
{
|
||||
{"umbracoPath", GlobalSettings.Path}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return JavaScript(ServerVariablesParser.Parse(d));
|
||||
|
||||
@@ -1,4 +1,10 @@
|
||||
using Umbraco.Web.Mvc;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Web;
|
||||
using System.Web.Http;
|
||||
using Umbraco.Core;
|
||||
using Umbraco.Web.Mvc;
|
||||
using Umbraco.Web.UI;
|
||||
using Umbraco.Web.WebApi;
|
||||
|
||||
namespace Umbraco.Web.Editors
|
||||
@@ -16,7 +22,7 @@ namespace Umbraco.Web.Editors
|
||||
/// </summary>
|
||||
public LegacyController()
|
||||
: this(UmbracoContext.Current)
|
||||
{
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -28,15 +34,30 @@ namespace Umbraco.Web.Editors
|
||||
{
|
||||
}
|
||||
|
||||
///// <summary>
|
||||
///// This will perform the delete operation for legacy items which include any item that
|
||||
///// has functionality included in the ui.xml structure.
|
||||
///// </summary>
|
||||
///// <returns></returns>
|
||||
//public HttpResponseMessage DeleteLegacyItem(string nodeId, string nodeType)
|
||||
//{
|
||||
|
||||
//}
|
||||
/// <summary>
|
||||
/// This will perform the delete operation for legacy items which include any item that
|
||||
/// has functionality included in the ui.xml structure.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public HttpResponseMessage DeleteLegacyItem(string nodeId, string nodeType, string nodeText)
|
||||
{
|
||||
//In order to process this request we MUST have an HttpContext available
|
||||
var httpContextAttempt = TryGetHttpContext();
|
||||
if (httpContextAttempt.Success)
|
||||
{
|
||||
int id;
|
||||
if (int.TryParse(nodeId, out id))
|
||||
{
|
||||
LegacyDialogHandler.Delete(httpContextAttempt.Result, UmbracoUser, nodeType, id, nodeText);
|
||||
return new HttpResponseMessage(HttpStatusCode.OK);
|
||||
}
|
||||
//We must have an integer id for this to work
|
||||
throw new HttpResponseException(HttpStatusCode.PreconditionFailed);
|
||||
}
|
||||
//We must have an HttpContext available for this to work.
|
||||
throw new HttpResponseException(HttpStatusCode.PreconditionFailed);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -37,6 +37,19 @@ namespace Umbraco.Web
|
||||
return url.GetUmbracoApiService(actionName, typeof (T));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return the Base Url (not including the action) for a Web Api service
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="url"></param>
|
||||
/// <param name="actionName"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetUmbracoApiServiceBaseUrl<T>(this UrlHelper url, string actionName)
|
||||
where T : UmbracoApiController
|
||||
{
|
||||
return url.GetUmbracoApiService<T>(actionName).TrimEnd(actionName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return the Url for a Web Api service
|
||||
/// </summary>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Web;
|
||||
using System.Web.Http;
|
||||
using Umbraco.Core;
|
||||
using Umbraco.Core.Services;
|
||||
@@ -21,6 +22,24 @@ namespace Umbraco.Web.WebApi
|
||||
Umbraco = new UmbracoHelper(umbracoContext);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to retreive the current HttpContext if one exists.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected Attempt<HttpContextBase> TryGetHttpContext()
|
||||
{
|
||||
object context;
|
||||
if (Request.Properties.TryGetValue("MS_HttpContext", out context))
|
||||
{
|
||||
var httpContext = context as HttpContext;
|
||||
if (httpContext != null)
|
||||
{
|
||||
return new Attempt<HttpContextBase>(true, new HttpContextWrapper(httpContext));
|
||||
}
|
||||
}
|
||||
return Attempt<HttpContextBase>.False;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the current ApplicationContext
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user