Skip to content

Commit cb4854a

Browse files
committed
[weex] enable extended constructors to use mixins
1 parent ddac976 commit cb4854a

File tree

9 files changed

+145
-55
lines changed

9 files changed

+145
-55
lines changed

flow/options.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ declare type ComponentOptions = {
6060
_renderChildren?: ?VNodeChildren;
6161
_componentTag: ?string;
6262
_scopeId: ?string;
63+
_base: Class<Component>;
6364
}
6465

6566
declare type PropOptions = {

src/core/global-api/assets.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function initAssetRegisters (Vue: GlobalAPI) {
2626
}
2727
if (type === 'component' && isPlainObject(definition)) {
2828
definition.name = definition.name || id
29-
definition = Vue.extend(definition)
29+
definition = this.options._base.extend(definition)
3030
}
3131
if (type === 'directive' && typeof definition === 'function') {
3232
definition = { bind: definition, update: definition }

src/core/global-api/extend.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ export function initExtend (Vue: GlobalAPI) {
1818
Vue.extend = function (extendOptions: Object): Function {
1919
extendOptions = extendOptions || {}
2020
const Super = this
21-
const isFirstExtend = Super.cid === 0
22-
if (isFirstExtend && extendOptions._Ctor) {
23-
return extendOptions._Ctor
21+
const SuperId = Super.cid
22+
const cachedCtors = extendOptions._Ctor || (extendOptions._Ctor = {})
23+
if (cachedCtors[SuperId]) {
24+
return cachedCtors[SuperId]
2425
}
2526
const name = extendOptions.name || Super.options.name
2627
if (process.env.NODE_ENV !== 'production') {
@@ -42,8 +43,10 @@ export function initExtend (Vue: GlobalAPI) {
4243
extendOptions
4344
)
4445
Sub['super'] = Super
45-
// allow further extension
46+
// allow further extension/mixin/plugin usage
4647
Sub.extend = Super.extend
48+
Sub.mixin = Super.mixin
49+
Sub.use = Super.use
4750
// create asset registers, so extended classes
4851
// can have their private assets too.
4952
config._assetTypes.forEach(function (type) {
@@ -59,9 +62,7 @@ export function initExtend (Vue: GlobalAPI) {
5962
Sub.superOptions = Super.options
6063
Sub.extendOptions = extendOptions
6164
// cache constructor
62-
if (isFirstExtend) {
63-
extendOptions._Ctor = Sub
64-
}
65+
cachedCtors[SuperId] = Sub
6566
return Sub
6667
}
6768
}

src/core/global-api/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ export function initGlobalAPI (Vue: GlobalAPI) {
3131
Vue.options[type + 's'] = Object.create(null)
3232
})
3333

34+
// this is used to identify the "base" constructor to extend all plain-object
35+
// components with in Weex's multi-instance scenarios.
36+
Vue.options._base = Vue
37+
3438
util.extend(Vue.options.components, builtInComponents)
3539

3640
initUse(Vue)

src/core/global-api/mixin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ import { mergeOptions } from '../util/index'
44

55
export function initMixin (Vue: GlobalAPI) {
66
Vue.mixin = function (mixin: Object) {
7-
Vue.options = mergeOptions(Vue.options, mixin)
7+
this.options = mergeOptions(this.options, mixin)
88
}
99
}

src/core/vdom/create-component.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* @flow */
22

3-
import Vue from '../instance/index'
43
import VNode from './vnode'
54
import { normalizeChildren } from './helpers/index'
65
import { resolveConstructorOptions } from '../instance/init'
@@ -23,8 +22,9 @@ export function createComponent (
2322
return
2423
}
2524

25+
const baseCtor = context.$options._base
2626
if (isObject(Ctor)) {
27-
Ctor = Vue.extend(Ctor)
27+
Ctor = baseCtor.extend(Ctor)
2828
}
2929

3030
if (typeof Ctor !== 'function') {
@@ -39,7 +39,7 @@ export function createComponent (
3939
if (Ctor.resolved) {
4040
Ctor = Ctor.resolved
4141
} else {
42-
Ctor = resolveAsyncComponent(Ctor, () => {
42+
Ctor = resolveAsyncComponent(Ctor, baseCtor, () => {
4343
// it's ok to queue this on every render because
4444
// $forceUpdate is buffered by the scheduler.
4545
context.$forceUpdate()
@@ -195,6 +195,7 @@ function destroy (vnode: MountedComponentVNode) {
195195

196196
function resolveAsyncComponent (
197197
factory: Function,
198+
baseCtor: Class<Component>,
198199
cb: Function
199200
): Class<Component> | void {
200201
if (factory.requested) {
@@ -207,7 +208,7 @@ function resolveAsyncComponent (
207208

208209
const resolve = (res: Object | Class<Component>) => {
209210
if (isObject(res)) {
210-
res = Vue.extend(res)
211+
res = baseCtor.extend(res)
211212
}
212213
// cache resolved
213214
factory.resolved = res

src/core/vdom/create-element.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export function createElement (
2121
return _createElement(this._self, tag, data, children)
2222
}
2323

24-
function _createElement (
24+
export function _createElement (
2525
context: Component,
2626
tag?: string | Class<Component> | Function | Object,
2727
data?: VNodeData,

src/entries/weex-framework.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Vue from 'weex/runtime/index'
22
import renderer from 'weex/runtime/config'
33

44
Vue.weexVersion = '2.0.5-weex.1'
5+
export { Vue }
56

67
const {
78
instances,
@@ -85,8 +86,11 @@ export function createInstance (
8586

8687
// Each instance has a independent `Vue` object and it should have
8788
// all top-level public APIs.
88-
const subVue = Vue.extend({});
89-
['util', 'set', 'delete', 'nextTick', 'use'].forEach(name => {
89+
const subVue = Vue.extend({})
90+
// ensure plain-object components are extended from the subVue
91+
subVue.options._base = subVue
92+
// expose global utility
93+
;['util', 'set', 'delete', 'nextTick'].forEach(name => {
9094
subVue[name] = Vue[name]
9195
})
9296

0 commit comments

Comments
 (0)