Skip to content

Commit

Permalink
feat(okam-core): support ref multiple instances
Browse files Browse the repository at this point in the history
  • Loading branch information
wuhy committed Nov 4, 2018
1 parent c5f5425 commit 80fbdb2
Showing 1 changed file with 41 additions and 21 deletions.
62 changes: 41 additions & 21 deletions packages/okam-core/src/base/component.js
Expand Up @@ -52,6 +52,42 @@ function normalizeEventArgs(args) {
return args;
}

/**
* Query the reference instance information by the given reference class
*
* @inner
* @param {string|Array.<string>} value the reference class
* @return {?Object|Array}
*/
function queryRefInstance(value) {
let isSelectAll = Array.isArray(value);
let result;

if (isSelectAll) {
let path = `.${value[0]}`;
if (typeof this.selectAllComponents === 'function') {
result = this.selectAllComponents(path);
}

if (!result) {
result = this.$selector.selectAll(path);
}
}
else {
let path = `.${value}`;
if (typeof this.selectComponent === 'function') {
result = this.selectComponent(path);
}

if (!result) {
// if not custom component, try to query element info by selector API
result = this.$selector.select(path);
}
}

return result;
}

/**
* Initialize the `$refs` value
*
Expand All @@ -70,30 +106,14 @@ function initRefs() {
}

let result = {};
let ctx;
let self = this;
let select = this.selectComponent;
let isSelectComponent = false;
if (typeof select === 'function') {
isSelectComponent = true;
ctx = self;
}
else {
ctx = this.$selector;
select = ctx.select;
}

const self = this;
Object.keys(refs).forEach(id => {
result[id] = {
get() {
let path = `.${refs[id]}`;
let result = select.call(ctx, path);
if (!result && isSelectComponent) {
// if not custom component, try to query element info by selector API
result = self.$selector.select(path);
}
return result;
}
let value = refs[id];
return queryRefInstance.call(self, value);
},
enumerable: true
};
});

Expand Down

0 comments on commit 80fbdb2

Please sign in to comment.