-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
77 lines (69 loc) · 2.01 KB
/
index.html
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
<html>
<head>
<script src="scripts/json2.min.js"></script>
<script src="scripts/uuid.js"></script>
<script src="scripts/jquery-1.10.2.min.js"></script>
<script src="scripts/require.js"></script>
<!-- <script src="scripts/aggregatesource.js"></script>
<script src="scripts/eventstore.js"></script>
<script src="scripts/model.js"></script> -->
<script>
require.config({
paths: {
'AggregateSource': 'scripts/aggregatesource',
'EventStore': 'scripts/eventstore',
'Model': 'scripts/model'
}
});
require(['Model'], function(Model) {
function prepareChangesForES(changes) {
var payload = [];
for(var index=0,length=changes.length;index<length;index++){
var change = changes[index];
payload.push({
"eventid": uuid.v4(),
"eventType": change.name,
"data": change.data
});
}
return payload;
}
function appendToStream(streamId, changes) {
return $.ajax({
type: 'POST',
url: 'http://127.0.0.1:2113/streams/' + streamId,
data: JSON.stringify(prepareChangesForES(changes), null, 2),
contentType: 'application/json'
});
}
var id = uuid.v4();
var repository = new Model.InventoryItemRepository(
{ baseUrl: 'http://localhost:2113/streams/' }
);
//First command
var original = new Model.InventoryItem(id, 'Boxer shorts');
for(var index=0;index<1000;index++){
original.checkIn(Math.floor((Math.random()*10)+1));
}
var originalChanges = original.getChanges();
for(var index = 0, length = originalChanges.length; index < length; index++) {
console.info(JSON.stringify(originalChanges[index]));
}
appendToStream(id, originalChanges).
done(function() {
repository.
get(id).
done(function(loaded) {
loaded.checkIn(2);
var newChanges = loaded.getChanges();
for(var index = 0, length = newChanges.length; index < length; index++) {
console.info(JSON.stringify(newChanges[index]));
}
appendToStream(id, newChanges).done(function() { console.info("Done with last ES write."); });
});
});
});
</script>
</head>
<body></body>
</html>