单页应用程序 (SPA) 可以充分利用已启用 HATEOAS 的 RESTful API,例如 SPA(带有 ui-rauter 的 angularJS 用于状态转换)
对于计算机到计算机的交互,我们宣传协议
通过在表示中嵌入链接来获取信息,就像我们在
人类网络。
在消费者-服务交互中:-
- 消费者向入口点提交初始请求
服务。
- 服务处理请求并使用填充了链接的资源表示进行响应。
- 消费者选择其中一个链接过渡到下一步
在互动中。
- 由于多次这样的交互消费者的进步
朝着它的目标前进。
示例代码说明
服务入口点
var applicationServices = angular.module('applicationServices', ['ngResource']);
userDetailServices.factory('DetailService', ['$resource',function($resource){
return $resource('api/users', {},{
query : {
method : 'GET',
headers : {'Accept': 'application/json'},
isArray: true
}
});
}]);
超媒体就是松耦合,我们在开发服务的时候
通过减少耦合从消费者那里抽象出细节,但是
不管松耦合的程度消费者必须有足够的
可用于与我们的服务交互的信息
假设api/users 是服务的入口点,并且是您的 SPA 知道的唯一 url,它将以填充有链接的资源表示进行响应,以进行进一步的交互。
{
"links": [
{
"rel": "self",
"href": "http://localhost:8080/persons{?page,size,sort}"
}
],
"users": [
{
"id": "3415NE11",
"firstName": "somefirstname",
"lastName": "lastname",
"emailAddress": "someemail",
"links": [
{
"rel": "section1",
"href": "http://localhost:8080/persons/3415NE11/section1
},
{
"rel": "section2",
"href": "http://localhost:8080/persons/3415NE11/section2
},
{
"rel": "gallery,
"href": "http://localhost:8080/filesRepo/profile/3415NE11/images
},
]
}
],
"page": {
"size": 20,
"totalElements": 2,
"totalPages": 1,
"number": 0
}
}
您的 SPA 从资源的部分信息开始,它将按需提供其他资源信息
- 具有部分用户信息的用户列表(在启动时,知道 url
之前)
- 用户资源信息Section1(按需,从1开始查找url
以上)
- Section2用户资源信息(点播,从1开始查找url
以上)
- 用户资源信息的Section-n(按需,从1开始查找url
以上)
带有 ui-router 导航
angular.module('userApp', [
'ui.bootstrap',
'ui.router',
'userControllers',
'userServices'
])
.config(
[ '$stateProvider', '$urlRouterProvider', '$httpProvider', function($stateProvider,$urlRouterProvider, $httpProvider) {
$stateProvider
.state('users', {
url: '/users-index',
templateUrl: 'partials/users-index.html',
resolve:{ // Your SPA needs this on start-up
DetailService:function(DetailService){
return DetailService.query();
}
},
controller:'UserController',
})
.state('users.section1', {
url: '/user-section1',
templateUrl: 'partials/user-section1.html',
})
.state('users.section2', {
url: '/users-section2',
templateUrl: 'partials/users.section2.html'
})
.state('users.gallery', {
url: '/users-gallery,
templateUrl: 'partials/users-gallery.html'
});
$urlRouterProvider.otherwise('/');
}])
.run([
'$rootScope',
'$location',
'$state',
'$stateParams'
function($rootScope, $location,$state, $stateParams) {
$rootScope.$state = $state;
$rootScope.$stateParams = $stateParams;
}
]);
您的 angularJS SPA 的用户控制器
(function() {
var userControllersApp = angular.module('userControllers', ['ngGeolocation']);
userControllersApp.controller('UserController',
['$scope',
'$rootScope',
'$http',
'$state',
'$filter',
'DetailService',
function($scope,$rootScope,$http,$state,$filter,DetailService) {
DetailService.$promise.then(function(result){
$scope.users = result.users;
});
$scope.userSection1= function(index){
var somelink = $filter('filter')($scope.users[index].links, { rel: "section1" })[0].href;
$http.get(somelink).success(function(data){
$state.go("users.section1");
});
// $http.post(somelink, data).success(successCallback);
};
$scope.userSection2= function(index){
var somelink = $filter('filter')($scope.users[index].links, { rel: "section2" })[0].href;
$http.get(somelink).success(function(data){
$state.go("users.section2");
});
// $http.post(somelink, data).success(successCallback);
};
$scope.userSection3= function(index){
var somelink = $filter('filter')($scope.users[index].links, { rel: "gallery" })[0].href;
$http.get(somelink).success(function(data){
$state.go("users.gallery");
});
// $http.post(somelink, data).success(successCallback);
};
} ]);
})();
超媒体的美妙之处在于它允许我们传达协议
作为
应用的资源表示
使用 $scope.users 嵌入链接进行进一步的交互。看看somelink 在section1()、section2() 和section(3) 函数中是如何被解析的。
您的 Angular SPA 导航(users-index.html)可能是
<div ng-repeat="user in users">
<label>{{user.firstname}}</label>
<button type="button" class="btn btn-xs " ng-click="section1($index)">Section1</button>
<button type="button" class="btn btn-xs " ng-click="section2($index)">Section2</button>
<button type="button" class="btn btn-xs " ng-click="section3($index)">Section3</button>
</div>
现在您的 SPA 的状态转换又依赖于服务器提供的动态链接来导航应用程序,这表明 SPA 可以充分利用启用 HATEOAS 的 RESTful API。 my apologies for the very long explanation ,hope it helps.