Skip to content

Commit

Permalink
feat(component): new $ref reactive variables
Browse files Browse the repository at this point in the history
  • Loading branch information
nolimits4web committed Sep 30, 2021
1 parent acab83e commit 108b63b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/core/modules/component/component-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,24 @@ class Component {
this.__onceEventHandlers.push({ eventName, handler });
}

getComponentRef() {
const self = this;
return (initialValue) => {
let value = initialValue;
const obj = {};
Object.defineProperty(obj, 'value', {
get() {
return value;
},
set(v) {
value = v;
self.update();
},
});
return obj;
};
}

getComponentStore() {
const { state, _gettersPlain, dispatch } = this.f7.store;
const $store = {
Expand Down Expand Up @@ -153,6 +171,7 @@ class Component {
$update: this.update.bind(this),
$emit: this.emit.bind(this),
$store: this.getComponentStore(),
$ref: this.getComponentRef(),
$el: {},
};
Object.defineProperty(ctx.$el, 'value', {
Expand Down
2 changes: 2 additions & 0 deletions src/core/modules/component/component.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export interface ComponentContext {
$el: {
value: Dom7Array;
};
/** Create reactive variable */
$ref: (initialValue: any) => { value: any };

/** 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. */
$tick: (callback?: () => void) => Promise<any>;
Expand Down
1 change: 1 addition & 0 deletions src/core/modules/component/parse-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ function parseComponent(componentString) {
var $theme = $$ctx.$theme;
var $update = $$ctx.$update;
var $store = $$ctx.$store;
var $ref = $$ctx.$ref;
return $h\`${template}\`
}
Expand Down

0 comments on commit 108b63b

Please sign in to comment.