-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreactive-property.js
106 lines (96 loc) · 2.36 KB
/
reactive-property.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
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
// #ReactiveProperty
// A simple class that provides an reactive property interface
/**
* @constructor
* @param {any} defaultValue Set the default value for the reactive property
*
* This api should only be in the internal.api.md
*/
ReactiveProperty = function(defaultValue) {
var self = this;
var _deps = new Deps.Dependency();
/** @property ReactiveProperty.value
* @private
* This contains the non reactive value, should only be used as a getter for
* internal use
*/
self.value = defaultValue;
self.onChange = function() {};
self.changed = function() {
_deps.changed();
self.onChange(self.value);
};
/**
* @method ReactiveProperty.get
* Usage:
* ```js
* var foo = new ReactiveProperty('bar');
* foo.get(); // equals "bar"
* ```
*/
self.get = function() {
_deps.depend();
return self.value;
};
/**
* @method ReactiveProperty.set Set property to value
* @param {any} value
* Usage:
* ```js
* var foo = new ReactiveProperty('bar');
* foo.set('bar');
* ```
*/
self.set = function(value) {
if (self.value !== value) {
self.value = value;
self.changed();
}
};
/**
* @method ReactiveProperty.dec Decrease numeric property
* @param {number} [by=1] Value to decrease by
* Usage:
* ```js
* var foo = new ReactiveProperty('bar');
* foo.set(0);
* foo.dec(5); // -5
* ```
*/
self.dec = function(by) {
self.value -= by || 1;
self.changed();
};
/**
* @method ReactiveProperty.inc increase numeric property
* @param {number} [by=1] Value to increase by
* Usage:
* ```js
* var foo = new ReactiveProperty('bar');
* foo.set(0);
* foo.inc(5); // 5
* ```
*/
self.inc = function(by) {
self.value += by || 1;
self.changed();
};
/**
* @method ReactiveProperty.getset increase numeric property
* @param {any} [value] Value to set property - if undefined the act like `get`
* @returns {any} Returns value if no arguments are passed to the function
* Usage:
* ```js
* var foo = new ReactiveProperty('bar');
* foo.getset(5);
* foo.getset(); // returns 5
* ```
*/
self.getset = function(value) {
if (typeof value !== 'undefined') {
self.set(value);
} else {
return self.get();
}
};
};