-
Notifications
You must be signed in to change notification settings - Fork 29.9k
/
odata-tests.ts
151 lines (112 loc) · 4.11 KB
/
odata-tests.ts
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import * as o from 'odata';
import * as Q from "q";
interface Product {
ID : number;
Name : string;
Description : string;
ReleaseDate : string;
DiscontinuedDate : Date;
Rating: number;
Price: number;
}
interface Category {
ID : number;
Name : string;
}
// copy pasta all the examples from the readme!
o('http://services.odata.org/V4/OData/OData.svc/Products')
.get<Product>(function(data) {
console.log(data); //returns an array of Product data
});
o('http://services.odata.org/V4/OData/OData.svc/Products')
.take(5)
.skip(2)
.get<Product>(function(data) {
console.log(data); //An array of 5 products skiped by 2
});
o('http://services.odata.org/V4/OData/OData.svc/Products')
.find(':0')
.route<Product>('Product/Detail/:0/:1',function(data) {
console.log('Route Product/Detail/'+this.param[0]+'/'+this.param[1]+' triggered. Result:');
console.log(data);
});
var oHandler = o('http://services.odata.org/V4/OData/OData.svc/Products');
//do somehtting
oHandler.find(1);
// do some more................
//get the data
oHandler.get<Product>(function(data) {
console.log(data);
//or the saved var also contains the data:
console.log(oHandler.data);
});
Q.all<any>([
o('http://services.odata.org/V4/OData/OData.svc/Products(4)').get<Product>(),
o('http://services.odata.org/V4/OData/OData.svc/Categories').take(2).get<Category[]>()
]).then(function(oHandlerArray) {
//The oHandler array contains the Product oHandler and the Group oHandler:
console.log(oHandlerArray[0].data); // 1 Product with id 4
console.log(oHandlerArray[1].data.length); // 2 Categories
});
o('http://services.odata.org/V4/OData/OData.svc/Products(2)')
.get<Product>()
.then(function(oHandler) {
console.log(oHandler.data);
}).fail(function(ex) {
console.log(ex);
});
o('http://services.odata.org/V4/OData/OData.svc/Products')
.post({Name:'Example 1',Description:'a'})
.post({Name:'Example 2',Description:'b'})
.save<Product>(function(data) {
console.log("Two Products added");
});
o('http://services.odata.org/V4/OData/OData.svc/Products(1)')
.patch({Name:'NewName'})
.save<Product>(function(data) {
console.log("Product Name changed");
});
o('http://services.odata.org/V4/OData/OData.svc/Products(1)')
.remove()
.save<Product>(function(data) {
console.log("Product deleted");
});
o('http://services.odata.org/V4/OData/OData.svc/Products(1)')
.ref('Categories', 2)
.save<Product>(function(data) {
console.log("Product(1) associated with Categories(2)");
});
o('http://services.odata.org/V4/OData/OData.svc/Products')
.find(2)
.get<Product>()
.then(function(oHandler) {
oHandler.data.Name="NewName";
return(o.save<Product>());
}).then(function(oHandler) {
console.log(oHandler.data.Name); //NewName
}).fail(function(ex) {
console.log("error");
});
// set an endpoint
o().config({
endpoint:'http://services.odata.org/V4/OData/OData.svc'
});
// after you have set an endpoint, you can shorten your queries:
o('Products').get<Product>(function(data) {
//same result like the first example on this page
});
//basic config
o().config({
endpoint:null, // your odata endpoint for the service
format: 'json',
autoFormat:false, // currently only json is supported
version:4, // oData version (currently supported version 4. However most also work with version 3.)
strictMode:true, // strict mode throws exception, non strict mode only logs them
start:null, // a function which is executed on loading
ready:null, // a function which is executed on ready
error:null, // a function which is executed on error
headers:[{name: '', value: ''}], // a array of additional headers e.g.: [{name:'headername',value:'headervalue'}]
username:null, // a basic auth username
password:null, // a basic auth password
isAsync:true //set this to false to make synced (a)jax calls. (dosn't work with basic auth!)
});