Skip to content

Commit

Permalink
feat(core): support ref
Browse files Browse the repository at this point in the history
  • Loading branch information
dlhandsome authored and Gcaufy committed Sep 19, 2018
1 parent 901dd74 commit 85a2603
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
12 changes: 12 additions & 0 deletions packages/core/dist/wepy.js
Original file line number Diff line number Diff line change
Expand Up @@ -1749,8 +1749,19 @@ function patchMethods (output, methods, isComponent) {

target._initComponent = function (e) {
var child = e.detail;
var ref = e.target.dataset.ref;
var vm = this.$wepy;
vm.$children.push(child);
if (ref) {
if (vm.$refs[ref]) {
warn(
'duplicate ref "' + ref +
'" will be covered by the last instance.\n',
vm
);
}
vm.$refs[ref] = child;
}
child.$parent = vm;
child.$app = vm.$app;
child.$root = vm.$root;
Expand Down Expand Up @@ -1868,6 +1879,7 @@ function patchLifecycle (output, options, rel, isComponent) {

vm.$dirty = new Dirty('path');
vm.$children = [];
vm.$refs = {};

this.$wepy = vm;
vm.$wx = this;
Expand Down
1 change: 1 addition & 0 deletions packages/core/weapp/init/lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export function patchLifecycle (output, options, rel, isComponent) {

vm.$dirty = new Dirty('path');
vm.$children = [];
vm.$refs = {};

this.$wepy = vm;
vm.$wx = this;
Expand Down
13 changes: 12 additions & 1 deletion packages/core/weapp/init/methods.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Event from '../class/Event';
import { isFunc, isUndef, parseModel } from './../util/index';
import { isFunc, isUndef, parseModel, warn } from './../util/index';

const eventHandler = function (method, fn) {
let methodKey = method.toLowerCase();
Expand Down Expand Up @@ -117,8 +117,19 @@ export function patchMethods (output, methods, isComponent) {

target._initComponent = function (e) {
let child = e.detail;
let ref = e.target.dataset.ref;
let vm = this.$wepy;
vm.$children.push(child);
if (ref) {
if (vm.$refs[ref]) {
warn(
'duplicate ref "' + ref +
'" will be covered by the last instance.\n',
vm
)
}
vm.$refs[ref] = child;
}
child.$parent = vm;
child.$app = vm.$app;
child.$root = vm.$root;
Expand Down

0 comments on commit 85a2603

Please sign in to comment.