Revert "Form: remove emitter (#4532)"

This reverts commit fff7dddd94.
pull/4626/head
Leopoldthecoder 2017-05-03 11:50:17 +08:00 committed by 杨奕
parent aff079d686
commit 0da3967d3a
4 changed files with 36 additions and 38 deletions

View File

@ -1,16 +1,12 @@
<script> <script>
import Emitter from 'element-ui/src/mixins/emitter';
export default { export default {
name: 'ElCheckboxGroup', name: 'ElCheckboxGroup',
componentName: 'ElCheckboxGroup', componentName: 'ElCheckboxGroup',
provide() { mixins: [Emitter],
return {
ElCheckboxGroup: this
};
},
inject: ['ElFormItem'],
props: { props: {
value: {}, value: {},
@ -23,7 +19,7 @@
watch: { watch: {
value(value) { value(value) {
this.ElFormItem.$emit('el.form.change', value); this.dispatch('ElFormItem', 'el.form.change', [value]);
} }
} }
}; };

View File

@ -40,9 +40,13 @@
</label> </label>
</template> </template>
<script> <script>
import Emitter from 'element-ui/src/mixins/emitter';
export default { export default {
name: 'ElCheckbox', name: 'ElCheckbox',
mixins: [Emitter],
componentName: 'ElCheckbox', componentName: 'ElCheckbox',
data() { data() {
@ -52,8 +56,6 @@
}; };
}, },
inject: ['ElCheckboxGroup'],
computed: { computed: {
model: { model: {
get() { get() {
@ -65,16 +67,17 @@
set(val) { set(val) {
if (this.isGroup) { if (this.isGroup) {
let isLimitExceeded = false; let isLimitExceeded = false;
(this.group.min !== undefined && (this._checkboxGroup.min !== undefined &&
val.length < this.group.min && val.length < this._checkboxGroup.min &&
(isLimitExceeded = true)); (isLimitExceeded = true));
(this.group.max !== undefined && (this._checkboxGroup.max !== undefined &&
val.length > this.group.max && val.length > this._checkboxGroup.max &&
(isLimitExceeded = true)); (isLimitExceeded = true));
isLimitExceeded === false && isLimitExceeded === false &&
this.group.$emit('input', val); this.dispatch('ElCheckboxGroup', 'input', [val]);
} else if (this.value !== undefined) { } else if (this.value !== undefined) {
this.$emit('input', val); this.$emit('input', val);
} else { } else {
@ -93,16 +96,21 @@
} }
}, },
group() {
return this.ElCheckboxGroup;
},
isGroup() { isGroup() {
return !!this.group; let parent = this.$parent;
while (parent) {
if (parent.$options.componentName !== 'ElCheckboxGroup') {
parent = parent.$parent;
} else {
this._checkboxGroup = parent;
return true;
}
}
return false;
}, },
store() { store() {
return this.group ? this.group.value : this.value; return this._checkboxGroup ? this._checkboxGroup.value : this.value;
} }
}, },
@ -132,7 +140,7 @@
this.$emit('change', ev); this.$emit('change', ev);
if (this.isGroup) { if (this.isGroup) {
this.$nextTick(_ => { this.$nextTick(_ => {
this.group.$emit('change', this.group.value); this.dispatch('ElCheckboxGroup', 'change', [this._checkboxGroup.value]);
}); });
} }
} }

View File

@ -17,6 +17,7 @@
</template> </template>
<script> <script>
import AsyncValidator from 'async-validator'; import AsyncValidator from 'async-validator';
import emitter from 'element-ui/src/mixins/emitter';
function noop() {} function noop() {}
@ -48,13 +49,7 @@
componentName: 'ElFormItem', componentName: 'ElFormItem',
provide() { mixins: [emitter],
return {
ElFormItem: this
};
},
inject: ['ElForm'],
props: { props: {
label: String, label: String,
@ -98,7 +93,11 @@
return ret; return ret;
}, },
form() { form() {
return this.ElForm; var parent = this.$parent;
while (parent.$options.componentName !== 'ElForm') {
parent = parent.$parent;
}
return parent;
}, },
fieldValue: { fieldValue: {
cache: false, cache: false,
@ -199,7 +198,8 @@
}, },
mounted() { mounted() {
if (this.prop) { if (this.prop) {
this.ElForm.$emit('el.form.addField', this); this.dispatch('ElForm', 'el.form.addField', [this]);
let initialValue = this.fieldValue; let initialValue = this.fieldValue;
if (Array.isArray(initialValue)) { if (Array.isArray(initialValue)) {
initialValue = [].concat(initialValue); initialValue = [].concat(initialValue);
@ -223,7 +223,7 @@
} }
}, },
beforeDestroy() { beforeDestroy() {
this.ElForm.$emit('el.form.removeField', this); this.dispatch('ElForm', 'el.form.removeField', [this]);
} }
}; };
</script> </script>

View File

@ -12,12 +12,6 @@
componentName: 'ElForm', componentName: 'ElForm',
provide() {
return {
ElForm: this
};
},
props: { props: {
model: Object, model: Object,
rules: Object, rules: Object,