-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket-emit.html
64 lines (59 loc) · 1.77 KB
/
socket-emit.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
<link rel="import" href="../polymer/polymer.html">
<dom-module id="socket-emit">
<template>
</template>
<script>
/**
* The `socket-emit` element builds off of a connection that was set up using `<socket-connect>`.
*
* It will emit the data provided with an event named after the provided name on the parent socket.
* It will also emit the data anytime it changes.
*
* Example:
*
* <socket-emit name="demo" data="[[emittedData]]"></socket-emit>
*
* @demo demo/index.html
*/
Polymer({
is: 'socket-emit',
properties: {
/**
* `name` is the name of the event to handle
*/
name: String,
/**
* `data` represents the sent data with the socket event
*/
data: {
type: Object,
notify: true,
reflectToAttribute: true,
observer: "_dataChanged"
}
},
/**
* Emits the event and sets up future values to be able to interact with sockets
*/
_setup: function(socket, token){
// Add the token to data if specified
// Do not overwrite already defined data.token
if( token != null && typeof(this.data) != undefined && typeof(this.data.token) == undefined){
this.data.token = token;
this.token = token;
}
this.socket = socket;
socket.emit(this.name, this.data);
},
/**
* Handles data changes to emit a new socket event
*/
_dataChanged: function(newVal, oldVal){
if( this.token != null && typeof(newVal) != undefined && typeof(newVal.token) == undefined){
newVal.token = token;
}
this.socket.emit(this.name, newVal);
}
});
</script>
</dom-module>