Skip to content

Commit 108b63b

Browse files
committed
feat(component): new $ref reactive variables
1 parent acab83e commit 108b63b

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

src/core/modules/component/component-class.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,24 @@ class Component {
118118
this.__onceEventHandlers.push({ eventName, handler });
119119
}
120120

121+
getComponentRef() {
122+
const self = this;
123+
return (initialValue) => {
124+
let value = initialValue;
125+
const obj = {};
126+
Object.defineProperty(obj, 'value', {
127+
get() {
128+
return value;
129+
},
130+
set(v) {
131+
value = v;
132+
self.update();
133+
},
134+
});
135+
return obj;
136+
};
137+
}
138+
121139
getComponentStore() {
122140
const { state, _gettersPlain, dispatch } = this.f7.store;
123141
const $store = {
@@ -153,6 +171,7 @@ class Component {
153171
$update: this.update.bind(this),
154172
$emit: this.emit.bind(this),
155173
$store: this.getComponentStore(),
174+
$ref: this.getComponentRef(),
156175
$el: {},
157176
};
158177
Object.defineProperty(ctx.$el, 'value', {

src/core/modules/component/component.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ export interface ComponentContext {
3232
$el: {
3333
value: Dom7Array;
3434
};
35+
/** Create reactive variable */
36+
$ref: (initialValue: any) => { value: any };
3537

3638
/** Defer the callback to be executed after the next DOM update cycle. Use it immediately after you’ve changed some data to wait for the DOM update. */
3739
$tick: (callback?: () => void) => Promise<any>;

src/core/modules/component/parse-component.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ function parseComponent(componentString) {
6060
var $theme = $$ctx.$theme;
6161
var $update = $$ctx.$update;
6262
var $store = $$ctx.$store;
63+
var $ref = $$ctx.$ref;
6364
6465
return $h\`${template}\`
6566
}

0 commit comments

Comments
 (0)