1 | <!DOCTYPE html>
|
2 | <html>
|
3 | <head>
|
4 | <title>eventric tutorial</title>
|
5 | </head>
|
6 | <body>
|
7 |
|
8 | <script type="text/javascript" src="node_modules/socket.io/node_modules/socket.io-client/socket.io.js" ></script>
|
9 | <script type="text/javascript" src="node_modules/eventric/dist/release/eventric.js" ></script>
|
10 | <script type="text/javascript" src="node_modules/eventric-remote-socketio-client/dist/release/eventric_remote_socketio_client.js" ></script>
|
11 |
|
12 | <script type="text/javascript">
|
13 | var socket = io.connect('http://localhost:1234');
|
14 | socketIORemoteClient = window['eventric-remote-socketio-client'];
|
15 | socketIORemoteClient.initialize({
|
16 | ioClientInstance: socket
|
17 | })
|
18 | .then(function() {
|
19 | var todoContext = eventric.remote('Todo');
|
20 | todoContext.setClient(socketIORemoteClient);
|
21 |
|
22 |
|
23 | var todoProjection = {
|
24 | initialize: function(params, done) {
|
25 | document.querySelector('body').innerHTML = '<h1>Todos</h1><div class="todos"></div>';
|
26 | done()
|
27 | },
|
28 |
|
29 | handleTodoCreated: function(domainEvent) {
|
30 | console.log('handle todo created')
|
31 | todoElement = document.createElement('div');
|
32 | todoElement.setAttribute('id', domainEvent.aggregate.id);
|
33 | todoElement.innerHTML = domainEvent.payload.title + '(' + domainEvent.aggregate.id + ')';
|
34 | document.querySelector('.todos').appendChild(todoElement);
|
35 | }
|
36 | };
|
37 |
|
38 | todoContext.initializeProjection(todoProjection, {});
|
39 |
|
40 | todoContext.command('CreateTodo', {title: 'My first todo'})
|
41 | .then(function(todoId) {
|
42 | console.log('Todo created: ', todoId);
|
43 | })
|
44 | .catch(function(error) {
|
45 | console.error('Error creating todo: ', error);
|
46 | });
|
47 | });
|
48 | </script>
|
49 | </body>
|
50 | </html>
|