Skip to content
This repository has been archived by the owner on Jan 26, 2023. It is now read-only.

Commit

Permalink
feat: extend Vue interface type and simplify internal type definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesLamberts committed Apr 20, 2019
1 parent 5b20717 commit 5b2124b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
19 changes: 8 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,16 @@ import {
} from 'vue'
import { Vue } from 'vue/types/vue'
import Registry from './registry'
import {
ModelInstallOptions,
VueModelProvided,
} from './types'
import { ModelInstallOptions } from './types'

export { Registry }

function resolveInjection(this: VueModelProvided, key: string) {
function resolveInjection(this: Vue, key: string) {
// tslint:disable-next-line no-this-assignment
let source = this
let ressource: VueModelProvided | null = null
let ressource: Vue | null = null

while (source = (source.$parent as VueModelProvided)) {
while (source = source.$parent) {
if (source.$modelsProvidedKeys && source.$modelsProvidedKeys.includes(key)) {
ressource = source
break
Expand All @@ -37,7 +34,7 @@ function resolveInjection(this: VueModelProvided, key: string) {
}

function createModelOptions(
vmHost: VueModelProvided,
vmHost: Vue,
name: string,
options: ComponentOptions<Vue>,
mergeMixins: Required<ComponentOptions<Vue>>['mixins'],
Expand Down Expand Up @@ -107,19 +104,19 @@ export default {
const installOptions: ModelInstallOptions = Object.assign({}, OPTIONS_DEFAULTS, rawInstallOptions)

vue.mixin({
beforeCreate(this: VueModelProvided) {
beforeCreate(this: Vue) {
const { modelRegistry, injectModels } = this.$options
this.$modelRegistry = this === this.$root
? (modelRegistry || new Registry(installOptions.restoreOnReplace))
: (<VueModelProvided>this.$root).$modelRegistry
: this.$root.$modelRegistry

if (injectModels) {
injectModels.forEach((inject) => {
resolveInjection.call(this, inject)
})
}
},
created(this: VueModelProvided) {
created(this: Vue) {
createModels.call(this, vue, installOptions)
},
})
Expand Down
25 changes: 17 additions & 8 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { ComponentOptions } from 'vue/types/options'
import { Vue } from 'vue/types/vue'
import {
Vue,
VueConstructor,
} from 'vue/types/vue'
import Registry from './registry'

declare module 'vue/types/options' {
Expand All @@ -12,20 +15,26 @@ declare module 'vue/types/options' {
}
}

export interface VueModelMap {
[key: string]: ComponentOptions<Vue>,
declare module 'vue/types/vue' {
// Global properties can be declared
// on the `VueConstructor` interface
interface Vue {
$modelsProvidedKeys?: string[],
$modelsProvided: { [key: string]: Vue },
$modelRegistry: Registry,
}
}

export interface VueModelProvided extends Vue {
$modelsProvidedKeys: string[],
$modelsProvided: { [key: string]: Vue },
$modelRegistry: Registry,
export interface VueModelMap {
[key: string]: ComponentOptions<Vue> | VueConstructor,
}

export interface ModelInstallOptions {
mixins: Required<ComponentOptions<Vue>>['mixins'],
restoreOnReplace: boolean,
globalModels: VueModelMap,
globalModels: {
[key: string]: ComponentOptions<Vue>,
},
}

export interface Export {
Expand Down

0 comments on commit 5b2124b

Please sign in to comment.