-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeathers-element.html
262 lines (223 loc) · 5.53 KB
/
feathers-element.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<link rel="import" href="../polymer/polymer.html">
<script type="text/javascript" src="../socket.io-client/socket.io.js"></script>
<!--
`feathers-element` is a polymer client for [feathers.js](http://feathersjs.com/).
To test the demo, run `node index.js` to start the API server.
@demo demo/index.html
-->
<script>
Polymer({
is: 'feathers-element',
properties: {
/**
* The data interface for binding.
*/
data: {
type: Array,
value: function() {
return []
},
readOnly: true,
notify: true
},
/**
* Server address to connect
*/
url: {
type: String,
value: '/',
reflectToAttribute: true,
},
/**
* Service API path. e.g. service="api/user"
*/
service: {
type: String,
notify: true,
reflectToAttribute: true
},
/**
* unique key in the retrieved object
*/
key: {
type: String,
reflectToAttribute: true,
value: "id"
},
/**
* query object
**/
query: {
type: Object,
notify: true,
value: {},
reflectToAttribute: true
},
// /**
// * number of items in data.
// * 0: unlimited;
// * positive number: limit to first result;
// * negative number: limit to last result;
// **/
// limit: {
// type: Number,
// value: 0,
// reflectToAttribute: true,
// }
},
/**
* Events
*/
created: function() {
this._socket = io();
},
attached: function() {
// observer is not guaranteed run before ready
this.startQuery();
},
/**
* Observers
**/
observers: [
'_urlChangeObserver(url, service)',
'_queryChangeObserver(query)',
],
_urlChangeObserver: function(url, service) {
this._socket.connect('url');
this._socket.on(this.service + ' created', this._createdHandler.bind(this));
this._socket.on(this.service + ' updated', this._updatedHandler.bind(this));
this._socket.on(this.service + ' patched', this._updatedHandler.bind(this));
this._socket.on(this.service + ' removed', this._removedHandler.bind(this));
},
_queryChangeObserver: function(query) {
if (!this.isAttached) {
return;
}
if (typeof query == "string") {
if (query == "") {
query = {};
}
else {
try {
query = JSON.parse(query);
} catch (e) {
throw new Error ("Can not parse query string " + query);
}
}
}
this._findCRUD(query, this._queryCallback.bind(this));
},
/**
* Methods
*/
/**
* add new element.
*/
add: function(data) {
this._createCRUD(data, this._createCallback);
},
/**
* Remove the item by providing the reference
**/
remove: function(item) {
var key = item[this.key];
this._removeCRUD(key, {} /*, remove callback*/ );
},
/**
* Execute query. if query object is not provided, query property will be used
*/
startQuery: function() {
var query;
if (arguments[0])
query = arguments[0];
else
query = this.query;
this._queryChangeObserver(query);
},
/**
* Update a item
**/
update: function(item, data) {
var key = item[this.key];
this._patchCRUD(key, data /*, update callback */ );
},
/**
* CRUD
*/
_findCRUD: function(param, cb) {
this._socket.emit(this.service + "::find", param, cb);
},
_getCRUD: function(id, param, cb) {
this._socket.emit(this.service + "::get", id, param, cb);
},
_createCRUD: function(param, cb) {
this._socket.emit(this.service + "::create", param, cb);
},
_removeCRUD: function(id, param, cb) {
this._socket.emit(this.service + "::remove", id, param, cb);
},
_updateCRUD: function(id, param, cb) {
this._socket.emit(this.service + "::update", id, param, cb);
},
_patchCRUD: function(id, param, cb) {
this._socket.emit(this.service + "::patch", id, param, cb);
},
/**
* push handlers
*/
/**
* Default handler when new item is created
**/
_createdHandler: function(doc) {
if (doc) {
this.push('data', doc);
}
},
/**
* Default handler for updated and patched
**/
_updatedHandler: function(doc) {
//updatedFilter
for (var i = 0; i < this.data.length; i++) {
if (this.data[i][this.key] == doc[this.key]) {
this.set('data.' + i, doc);
return; // only replace the first found
}
}
// nothing found, need add to local?
},
_removedHandler: function(doc) {
for (var i = 0; i < this.data.length; i++) {
if (this.data[i][this.key] == doc[this.key]) {
this.splice('data', i, 1);
}
}
},
/**
* default handlers
*/
/**
* Callback function of query
*/
_queryCallback: function(err, doc) {
if (err) {
this.fire('error', err);
return;
}
// queryFilterCallback
this._setData(doc);
},
_createCallback: function(err, doc) {
if (err) {
this.fire('error', err);
return;
}
}
/**
* Triggered when socket.io communication raises an error.
*
* @event error
* @param {object} error object
*/
})
</script>