-
Notifications
You must be signed in to change notification settings - Fork 5
/
entity-loading-behavior.js
81 lines (73 loc) · 1.93 KB
/
entity-loading-behavior.js
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
import './store/entity-behavior.js';
import './store/entity-store.js';
// 'use strict';
window.D2L = window.D2L || {};
window.D2L.PolymerBehaviors = window.D2L.PolymerBehaviors || {};
window.D2L.PolymerBehaviors.Siren = window.D2L.PolymerBehaviors.Siren || {};
/*
* @polymerBehavior
*/
D2L.PolymerBehaviors.Siren.EntityLoadingBehaviorImpl = {
properties: {
loading: {
type: Boolean,
value: true
},
error: {
type: Boolean,
value: false
},
fetched: {
type: Boolean,
value: false
}
},
observers: [
'_updateListeners(href, token)'
],
_updateListeners: function(href, token) {
this.error = false;
this.loading = true;
this.fetched = false;
if (!this._boundUpdateLoadingState) {
this._boundUpdateLoadingState = this._updateLoadingState.bind(this);
}
if (this._oldHref && this._oldToken) {
window.D2L.Siren.EntityStore.removeListener(this._oldHref, this._oldToken, this._boundUpdateLoadingState);
}
this._oldHref = href;
this._oldToken = token;
if (typeof href === 'string' && (typeof token === 'string' || typeof token === 'function')) {
window.D2L.Siren.EntityStore.addListener(href, token, this._boundUpdateLoadingState);
window.D2L.Siren.EntityStore.fetch(href, token)
.then(function(entity) {
this._updateLoadingState(entity);
}.bind(this))
.catch(function(error) {
this._updateLoadingState(null, error);
}.bind(this));
}
},
attached: function() {
this._updateListeners(this.href, this.token);
},
detached: function() {
this._updateListeners();
},
_updateLoadingState: function(entity, error) {
if (error) {
this.error = true;
this.loading = false;
this.fetched = false;
} else if (entity) {
this.error = false;
this.loading = false;
this.fetched = true;
}
}
};
/** @polymerBehavior */
D2L.PolymerBehaviors.Siren.EntityLoadingBehavior = [
D2L.PolymerBehaviors.Siren.EntityBehavior,
D2L.PolymerBehaviors.Siren.EntityLoadingBehaviorImpl
];