本文整理汇总了TypeScript中mobx-state-tree.types.late方法的典型用法代码示例。如果您正苦于以下问题:TypeScript types.late方法的具体用法?TypeScript types.late怎么用?TypeScript types.late使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mobx-state-tree.types
的用法示例。
在下文中一共展示了types.late方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: test
test("can time travel with Mutable object", t => {
const MutableUnion = types.union(
types.string,
types.boolean,
types.number,
types.map(types.late(() => MutableUnion)),
types.array(types.late(() => MutableUnion))
)
const MutableStoreModel = types
.model({
mutable: MutableUnion
})
.actions(self => {
setUndoManagerDifferentTree(self)
return {
setProp(k, v) {
self.mutable.set(k, v)
}
}
})
const store = MutableStoreModel.create({ mutable: {} })
const mutable = store.mutable
t.deepEqual(mutable.toJSON(), {})
t.is(undoManager.canUndo, false)
t.is(undoManager.canRedo, false)
t.is(undoManager.history.length, 0)
store.setProp("foo", 1)
t.deepEqual(mutable.toJSON(), { foo: 1 })
t.is(undoManager.canUndo, true)
t.is(undoManager.canRedo, false)
t.is(undoManager.history.length, 1)
store.setProp("foo", {})
t.deepEqual(mutable.toJSON(), { foo: {} })
t.is(undoManager.canUndo, true)
t.is(undoManager.canRedo, false)
t.is(undoManager.history.length, 2)
undoManager.undo()
t.deepEqual(mutable.toJSON(), { foo: 1 })
t.is(undoManager.canUndo, true)
t.is(undoManager.canRedo, true)
t.is(undoManager.history.length, 2)
undoManager.undo()
t.deepEqual(mutable.toJSON(), {})
t.is(undoManager.canUndo, false)
t.is(undoManager.canRedo, true)
t.is(undoManager.history.length, 2)
})
示例2: new
export function polymorphic<PROPS extends ModelProperties, OTHERS, TYPE>(Code: new() => TYPE, Model: IModelType<PROPS, OTHERS>, name?: string): IModelType<PROPS, TYPE> {
// Union of this class and all of its subclasses.
// Late evaluation allows subclasses to add themselves to the type list
// before any instances are created.
const Union: ClassyUnion<PROPS, TYPE> = types.late(() => types.union.apply(types, Union.$typeList)) as any;
// First item in the type list is a dispatcher function
// for parsing type tags in snapshots.
Union.$typeList = [ { dispatcher: (snap: any) =>
(snap && typeTag && snap[typeTag] && Union.$typeTbl[snap[typeTag]]) || Model
}Â ];
Union.$typeTbl = {};
Union.$proto = Code.prototype;
// Copy methods from model object into returned union,
// making it work like a regular model.
for(let mixin: { [key: string]: any } = Model; mixin; mixin = Object.getPrototypeOf(mixin)) {
for(let key of Object.getOwnPropertyNames(mixin)) {
const desc = Object.getOwnPropertyDescriptor && Object.getOwnPropertyDescriptor(mixin, key);
if(!desc || (desc.configurable && desc.writable && !desc.get && !desc.set)) {
const value = !(key in Union) && mixin[key];
if(typeof(value) == 'function') {
(Union as { [key: string]: any })[key] = function() {
return(value.apply(Model, arguments));
};
}
}
}
}
// Initialize union of allowed class substitutes with the class itself,
// and augment unions of all parent classes with this subclass,
// to allow polymorphism.
for(let Class = Union; Class; Class = Class.$proto.$parent) {
const typeList = Class.$typeList;
const typeTbl = Class.$typeTbl;
if(typeList) typeList.push(Model);
if(typeTbl && name) typeTbl[name] = Model;
}
return(Union);
}
示例3:
import { types } from 'mobx-state-tree';
export const NavItem = types.model('NavItem', {
id: types.identifier(types.string),
text: types.string,
url: types.optional(types.string, ''),
subTitle: types.optional(types.string, ''),
icon: types.optional(types.string, ''),
img: types.optional(types.string, ''),
active: types.optional(types.boolean, false),
hideFromTabs: types.optional(types.boolean, false),
breadcrumbs: types.optional(types.array(types.late(() => Breadcrumb)), []),
children: types.optional(types.array(types.late(() => NavItem)), []),
});
export const Breadcrumb = types.model('Breadcrumb', {
title: types.string,
url: types.string,
});
示例4:
ďťżimport { types } from 'mobx-state-tree';
export const NavItem = types.model('NavItem', {
id: types.identifier(types.string),
text: types.string,
url: types.optional(types.string, ''),
subTitle: types.optional(types.string, ''),
icon: types.optional(types.string, ''),
img: types.optional(types.string, ''),
active: types.optional(types.boolean, false),
children: types.optional(types.array(types.late(() => NavItem)), []),
});