-
Notifications
You must be signed in to change notification settings - Fork 5
/
hal.js
68 lines (56 loc) · 1.86 KB
/
hal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* global angular */
(function() {
/* global TraversonJsonHalAdapter */
'use strict';
var app =
angular.module('traverson-angular-hal-example',
['traverson', 'ngSanitize']);
var rootUri = '/';
app.controller('generalSetup', function($scope) {
$scope.code =
'// register HAL adapter in Traverson\'s media type registry<br>' +
'traverson<br>' +
'.registerMediaType(TraversonJsonHalAdapter.mediaType,' +
' TraversonJsonHalAdapter);<br>' +
'<br>' +
'var rootUri = \'' + rootUri + '\';<br>' +
'var api = traverson<br>' +
'.from(rootUri)<br>' +
'.jsonHal()<br>' +
'.withRequestOptions({ headers: { ' +
'\'accept\': \'application/hal+json\' } });';
});
app.service('apiService', function(traverson) {
// register HAL adapter in Traverson's media type registry
traverson
.registerMediaType(TraversonJsonHalAdapter.mediaType,
TraversonJsonHalAdapter);
var api = traverson
.from(rootUri)
.jsonHal()
.withRequestOptions({ headers: { 'accept': 'application/hal+json' } });
this.hal = function() {
return api
.follow('first', 'second', 'inside_second')
.getResource();
};
});
app.controller('halController', function($scope, apiService) {
$scope.start = function() {
$scope.response = '... talking to server, please stand by ...';
apiService.hal().result.then(function(resource) {
$scope.response = JSON.stringify(resource, null, 2);
}, function(err) {
$scope.response = err.message || JSON.stringify(err);
});
};
$scope.code =
'api<br>' +
'.follow(\'first\', \'second\', \'inside_second\')<br>' +
'.getResource()<br>' +
'.result<br>' +
'.then(function(resource) {<br>' +
' // do something with the resource...<br>' +
'});';
});
})();