forked from ibm-js/decor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInvalidating.js
73 lines (66 loc) · 2.39 KB
/
Invalidating.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
/** @module decor/Invalidating */
define([
"dcl/dcl",
"./Stateful",
"./Destroyable"
], function (dcl, Stateful, Destroyable) {
/**
* Mixin class for widgets
* that want to calculate computed properties at once and/or to render UI at once upon multiple property changes.
* @class module:decor/Invalidating
*/
var Invalidating = dcl([Stateful, Destroyable], /** @lends module:decor/Invalidating# */ {
constructor: dcl.after(function () {
this.initializeInvalidating();
}),
/**
* Sets up observers, one for computed properties, one for UI rendering.
* Normally this method is called automatically by the constructor, and should not be called manually,
* but the method is exposed for custom elements since they do not call the `constructor()` method.
* @protected
*/
initializeInvalidating: function () {
this.own(
this._hComputing = this.observe(function (oldValues) {
this.computeProperties(oldValues);
this.deliverComputing();
}),
this._hRendering = this.observe(function (oldValues) {
this.refreshRendering(oldValues);
})
);
// Discard changes made by this function itself (to ._hComputing and _hRendering)
this.discardChanges();
},
/**
* Synchronously deliver change records for computed properties
* so that `refreshingComputing()` is called if there are pending change records.
*/
deliverComputing: function () {
this._hComputing && this._hComputing.deliver();
return this._hComputing;
},
/**
* Discard change records for computed properties.
*/
discardComputing: function () {
this._hComputing && this._hComputing.discardChanges();
return this._hComputing;
},
/**
* Callback function to calculate computed properties upon property changes.
* @param {Object} newValues The hash table of new property values, keyed by property names.
* @param {Object} oldValues The hash table of old property values, keyed by property names.
*/
computeProperties: function () {},
/**
* Callback function to render UI upon property changes.
* @param {Object} newValues The hash table of new property values, keyed by property names.
* @param {Object} oldValues The hash table of old property values, keyed by property names.
*/
refreshRendering: function () {}
});
dcl.chainAfter(Invalidating, "computeProperties");
dcl.chainAfter(Invalidating, "refreshRendering");
return Invalidating;
});