mirror of
https://gitee.com/vant-contrib/vant-weapp.git
synced 2025-04-06 03:58:05 +08:00
feat: remove computed option support (#2002)
This commit is contained in:
parent
f178552d75
commit
31b7234864
@ -10,13 +10,12 @@ function mapKeys(source: object, target: object, map: object) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function VantComponent<Data, Props, Methods, Computed>(
|
function VantComponent<Data, Props, Methods>(
|
||||||
vantOptions: VantComponentOptions<
|
vantOptions: VantComponentOptions<
|
||||||
Data,
|
Data,
|
||||||
Props,
|
Props,
|
||||||
Methods,
|
Methods,
|
||||||
Computed,
|
CombinedComponentInstance<Data, Props, Methods>
|
||||||
CombinedComponentInstance<Data, Props, Methods, Computed>
|
|
||||||
> = {}
|
> = {}
|
||||||
): void {
|
): void {
|
||||||
const options: any = {};
|
const options: any = {};
|
||||||
|
@ -1,30 +1,25 @@
|
|||||||
import { Weapp } from './weapp';
|
import { Weapp } from './weapp';
|
||||||
|
|
||||||
type RecordToAny<T> = { [K in keyof T]: any };
|
type RecordToAny<T> = { [K in keyof T]: any };
|
||||||
type RecordToReturn<T> = {
|
|
||||||
[P in keyof T]: T[P] extends (...args: any[]) => any ? ReturnType<T[P]> : T[P]
|
|
||||||
};
|
|
||||||
|
|
||||||
export type CombinedComponentInstance<
|
export type CombinedComponentInstance<
|
||||||
Data,
|
Data,
|
||||||
Props,
|
Props,
|
||||||
Methods,
|
Methods
|
||||||
Computed
|
|
||||||
> = Methods &
|
> = Methods &
|
||||||
WechatMiniprogram.Component.TrivialInstance &
|
WechatMiniprogram.Component.TrivialInstance &
|
||||||
Weapp.FormField &
|
Weapp.FormField &
|
||||||
{
|
{
|
||||||
data: Data & RecordToReturn<Computed> & RecordToAny<Props>;
|
data: Data & RecordToAny<Props>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export interface VantComponentOptions<Data, Props, Methods, Computed, Instance> {
|
export interface VantComponentOptions<Data, Props, Methods, Instance> {
|
||||||
data?: Data;
|
data?: Data;
|
||||||
field?: boolean;
|
field?: boolean;
|
||||||
classes?: string[];
|
classes?: string[];
|
||||||
mixins?: string[];
|
mixins?: string[];
|
||||||
props?: Props & Weapp.PropertyOption;
|
props?: Props & Weapp.PropertyOption;
|
||||||
watch?: Weapp.WatchOption<Instance>;
|
watch?: Weapp.WatchOption<Instance>;
|
||||||
computed?: Computed & Weapp.ComputedOption<Instance>;
|
|
||||||
relation?: Weapp.RelationOption<Instance> & { name: string };
|
relation?: Weapp.RelationOption<Instance> & { name: string };
|
||||||
relations?: {
|
relations?: {
|
||||||
[componentName: string]: Weapp.RelationOption<Instance>;
|
[componentName: string]: Weapp.RelationOption<Instance>;
|
||||||
|
@ -1,56 +1,13 @@
|
|||||||
function setAsync(context: WechatMiniprogram.Component.TrivialInstance, data: object) {
|
|
||||||
return new Promise(resolve => {
|
|
||||||
context.setData(data, resolve);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export const behavior = Behavior({
|
export const behavior = Behavior({
|
||||||
created() {
|
|
||||||
if (!this.$options) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const cache = {};
|
|
||||||
const { computed } = this.$options();
|
|
||||||
const keys = Object.keys(computed);
|
|
||||||
|
|
||||||
this.calcComputed = () => {
|
|
||||||
const needUpdate = {};
|
|
||||||
keys.forEach(key => {
|
|
||||||
const value = computed[key].call(this);
|
|
||||||
|
|
||||||
if (cache[key] !== value) {
|
|
||||||
cache[key] = value;
|
|
||||||
needUpdate[key] = value;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return needUpdate;
|
|
||||||
};
|
|
||||||
},
|
|
||||||
|
|
||||||
attached() {
|
|
||||||
this.set();
|
|
||||||
},
|
|
||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
// set data and set computed data
|
|
||||||
set(data: object, callback: Function) {
|
set(data: object, callback: Function) {
|
||||||
const stack = [];
|
return new Promise(resolve => {
|
||||||
|
this.setData(data, () => {
|
||||||
if (data) {
|
if (callback && typeof callback === 'function') {
|
||||||
stack.push(setAsync(this, data));
|
callback.call(this);
|
||||||
}
|
}
|
||||||
|
resolve();
|
||||||
if (this.calcComputed) {
|
});
|
||||||
stack.push(setAsync(this, this.calcComputed()));
|
|
||||||
}
|
|
||||||
|
|
||||||
return Promise.all(stack).then(res => {
|
|
||||||
if (callback && typeof callback === 'function') {
|
|
||||||
callback.call(this);
|
|
||||||
}
|
|
||||||
return res;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
import { behavior } from './behavior';
|
import { behavior } from './behavior';
|
||||||
import { observeProps } from './props';
|
|
||||||
|
|
||||||
export function observe(vantOptions, options) {
|
export function observe(vantOptions, options) {
|
||||||
const { watch, computed } = vantOptions;
|
const { watch } = vantOptions;
|
||||||
|
|
||||||
options.behaviors.push(behavior);
|
options.behaviors.push(behavior);
|
||||||
|
|
||||||
@ -21,13 +20,4 @@ export function observe(vantOptions, options) {
|
|||||||
|
|
||||||
options.properties = props;
|
options.properties = props;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (computed) {
|
|
||||||
options.methods = options.methods || {};
|
|
||||||
options.methods.$options = () => vantOptions;
|
|
||||||
|
|
||||||
if (options.properties) {
|
|
||||||
observeProps(options.properties);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,25 +0,0 @@
|
|||||||
export function observeProps(props) {
|
|
||||||
if (!props) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Object.keys(props).forEach(key => {
|
|
||||||
let prop = props[key];
|
|
||||||
if (prop === null || !('type' in prop)) {
|
|
||||||
prop = { type: prop };
|
|
||||||
}
|
|
||||||
|
|
||||||
let { observer } = prop;
|
|
||||||
prop.observer = function (...args) {
|
|
||||||
if (observer) {
|
|
||||||
if (typeof observer === 'string') {
|
|
||||||
observer = this[observer];
|
|
||||||
}
|
|
||||||
observer.apply(this, args);
|
|
||||||
}
|
|
||||||
this.set();
|
|
||||||
};
|
|
||||||
|
|
||||||
props[key] = prop;
|
|
||||||
});
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user