init
This commit is contained in:
47
fuintUniapp/components/mixins/observer/behavior.js
Normal file
47
fuintUniapp/components/mixins/observer/behavior.js
Normal file
@@ -0,0 +1,47 @@
|
||||
function setAsync(context, data) {
|
||||
return new Promise(resolve => {
|
||||
context.setData(data, resolve);
|
||||
});
|
||||
}
|
||||
;
|
||||
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] = needUpdate[key] = value;
|
||||
}
|
||||
});
|
||||
return needUpdate;
|
||||
};
|
||||
},
|
||||
attached() {
|
||||
this.set();
|
||||
},
|
||||
methods: {
|
||||
// set data and set computed data
|
||||
set(data, callback) {
|
||||
const stack = [];
|
||||
if (data) {
|
||||
stack.push(setAsync(this, data));
|
||||
}
|
||||
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;
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
27
fuintUniapp/components/mixins/observer/index.js
Normal file
27
fuintUniapp/components/mixins/observer/index.js
Normal file
@@ -0,0 +1,27 @@
|
||||
import { behavior } from './behavior';
|
||||
import { observeProps } from './props';
|
||||
export function observe(vantOptions, options) {
|
||||
const { watch, computed } = vantOptions;
|
||||
options.behaviors.push(behavior);
|
||||
if (watch) {
|
||||
const props = options.properties || {};
|
||||
Object.keys(watch).forEach(key => {
|
||||
if (key in props) {
|
||||
let prop = props[key];
|
||||
if (prop === null || !('type' in prop)) {
|
||||
prop = { type: prop };
|
||||
}
|
||||
prop.observer = watch[key];
|
||||
props[key] = prop;
|
||||
}
|
||||
});
|
||||
options.properties = props;
|
||||
}
|
||||
if (computed) {
|
||||
options.methods = options.methods || {};
|
||||
options.methods.$options = () => vantOptions;
|
||||
if (options.properties) {
|
||||
observeProps(options.properties);
|
||||
}
|
||||
}
|
||||
}
|
||||
22
fuintUniapp/components/mixins/observer/props.js
Normal file
22
fuintUniapp/components/mixins/observer/props.js
Normal file
@@ -0,0 +1,22 @@
|
||||
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 () {
|
||||
if (observer) {
|
||||
if (typeof observer === 'string') {
|
||||
observer = this[observer];
|
||||
}
|
||||
observer.apply(this, arguments);
|
||||
}
|
||||
this.set();
|
||||
};
|
||||
props[key] = prop;
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user