-
Notifications
You must be signed in to change notification settings - Fork 0
/
object-sync.html
219 lines (181 loc) · 5.39 KB
/
object-sync.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
<link rel="import" href="bower_components/polymer/polymer.html">
<link rel="import" href="bower_components/iron-ajax/iron-ajax.html">
<!--
`object-sync` syncs an Object to a remote server.
You simply specify a URL where the JSON resource resides.
The URL should accept:
- `GET` requests for fetching the object
- `PUT` requests for saving the object
The payload for saving - `PUT` - would be included in the `body` of the request.
The `value` is saved to the server only if it changes *after* it was initially
fetched successfully from the remote server so you don't have to worry about
timing issues.
@demo demo/index.html
-->
<dom-module id="object-sync">
<template>
<style>
:host {
display: block;
}
.label {
font-family: Helvetica;
@apply --status-label;
}
.label.success {
color: #8BC34A;
@apply --status-label-success;
}
.label.error {
color: #F44336;
@apply --status-label-error;
}
</style>
<iron-ajax
id="ajaxGet"
method="GET"
url="[[url]]"
headers="[[headers]]"
last-error="{{lastError}}"
on-response="_gotRemoteValue"
on-error="_notifyAjaxError">
</iron-ajax>
<iron-ajax
id="ajaxPut"
method="PUT"
url="[[url]]"
headers="[[headers]]"
last-error="{{lastError}}"
on-response="_putRemoteValue"
on-error="_notifyAjaxError"
content-type="application/json">
</iron-ajax>
<span class="label success" hidden="[[!_syncSuccess]]">[[successMsg]]</span>
<span class="label error" hidden="[[!lastError]]">[[errorMsg]]</span>
</template>
<script>
Polymer({
is: "object-sync",
properties: {
/** The URL where the JSON resource resides */
url: {
type: String
},
/** The request headers for both `GET` & `PUT` */
headers: {
type: Object
},
/** The data associated with this storage */
value: {
type: Object,
notify: true
},
/** The last Ajax error, if any. */
lastError: {
type: Object,
value: false,
notify: true
},
/** Set to `true` to enable this element. If false no requests/syncing
will take place. Most commonly used to conditionally enable syncing
if for example a user is logged-in. Resetting this value to `true` will
re-trigger an initial fetch */
shouldFetch: {
type: Boolean,
value: false,
observer: "_fetchInitialObject"
},
/** Set to `true` to prevent all requests for this element */
disabled: {
type: Boolean,
value: false
},
/** Message to be displayed if object was synced succesfully */
successMsg: {
type: String,
value: "Saved"
},
/** Message to be displayed if any of the requests fail */
errorMsg: {
type: String,
value: "An error occured :("
},
_fetchedInitialValue: {
type: Boolean,
value: false,
readOnly: true
},
_syncSuccess: {
type: Boolean,
value: false,
readOnly: true
},
},
/**
* Fired when the initial value is succesfully fetched from the server
*
* @event value-fetched
*/
/**
* Fired when the value is succesfully saved to remote
*
* @event value-saved
*/
/**
* Fired when an Ajax error occurs
*
* @event error
* @param {Object} error The Ajax error event
*/
observers: [
"_valueChanged(value.*)"
],
attached: function() {
window.objectSync = this;
},
_valueChanged: function(changes) {
if (!this._fetchedInitialValue) return false;
this.debounce("_syncingValueChange", function() {
this._syncObjectToRemote(changes.base);
}, 750);
},
_fetchInitialObject: function(shouldFetch) {
if (!shouldFetch || this.disabled) return false;
/*
* `shouldFetch` prop observer might trigger many times, until it's set,
* for example if it's bound to a logged-in-user type variable, user
* would be first logged-out until the system logs him in
* (in AJAX-login mechanisms)
*
* @TODO investigate a better way of handling such issues
*/
this.debounce("_fetchInitialObject", function() {
this.$.ajaxGet.generateRequest();
}.bind(this));
},
_syncObjectToRemote: function(value) {
if (this.disabled) return false;
this.$.ajaxPut.body = JSON.stringify(value);
this.$.ajaxPut.generateRequest();
},
_gotRemoteValue: function(e) {
this.set("value", e.detail.response);
this._set_fetchedInitialValue(true);
this.fire("value-fetched", e.detail.response);
},
_putRemoteValue: function(e) {
this.fire("value-saved", e);
this._setSyncSuccess();
},
_setSyncSuccess: function() {
this._set_syncSuccess(true);
this.async(function() {
this._set_syncSuccess(false);
}, 1000);
},
_notifyAjaxError: function(e) {
this.fire("error", e);
}
});
</script>
</dom-module>