本文整理汇总了TypeScript中mobx-state-tree.types.maybe方法的典型用法代码示例。如果您正苦于以下问题:TypeScript types.maybe方法的具体用法?TypeScript types.maybe怎么用?TypeScript types.maybe使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mobx-state-tree.types
的用法示例。
在下文中一共展示了types.maybe方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: new
export function mstWithChildren<PROPS extends ModelProperties, OTHERS, TYPE>(
Code: new() => TYPE,
Data: IModelType<PROPS, OTHERS>,
name?: string
) {
const Children = types.array(types.late((): any => Model)) as IArrayType<
RecursiveCreationType<PROPS>,
RecursiveSnapshotType<PROPS>,
TYPE
>;
const Branch = (Data as any as IModelType<PROPS, TYPE>).props({
children: types.maybe(
Children as any as IComplexType<
RecursiveCreationType<PROPS>[],
RecursiveSnapshotType<PROPS>[],
IObservableArray<IModelType<PROPS, OTHERS>>
>
)
});
const Model = mst(Code, Branch, 'Node') as typeof Branch;
return({ Model, Children });
}
示例2: setComponentId
import { types } from 'mobx-state-tree';
const UI = types
.model('UI', {
componentId: types.maybe(types.string),
counter: types.optional(types.number, 0),
})
.actions(self => ({
setComponentId(componentId?: string) {
if (componentId) {
self.componentId = componentId;
}
},
increment() {
self.counter += 1;
},
}));
export default UI;
示例3: switch
USER: { value: 'User', text: 'User' },
VIEWER: { value: 'Viewer', text: 'Everyone With Viewer Role' },
EDITOR: { value: 'Editor', text: 'Everyone With Editor Role' },
};
export const aclTypes = Object.keys(aclTypeValues).map(item => aclTypeValues[item]);
const defaultNewType = aclTypes[0].value;
export const NewPermissionsItem = types
.model('NewPermissionsItem', {
type: types.optional(
types.enumeration(Object.keys(aclTypeValues).map(item => aclTypeValues[item].value)),
defaultNewType
),
userId: types.maybe(types.number),
userLogin: types.maybe(types.string),
userAvatarUrl: types.maybe(types.string),
teamAvatarUrl: types.maybe(types.string),
teamId: types.maybe(types.number),
team: types.maybe(types.string),
permission: types.optional(types.number, 1),
})
.views(self => ({
isValid: () => {
switch (self.type) {
case aclTypeValues.GROUP.value:
return self.teamId && self.team;
case aclTypeValues.USER.value:
return !!self.userId && !!self.userLogin;
case aclTypeValues.VIEWER.value:
示例4: Error
import _ from 'lodash';
import { types, getEnv } from 'mobx-state-tree';
import { NavItem } from './NavItem';
import { ITeam } from '../TeamsStore/TeamsStore';
export const NavStore = types
.model('NavStore', {
main: types.maybe(NavItem),
node: types.maybe(NavItem),
})
.actions(self => ({
load(...args) {
let children = getEnv(self).navTree;
let main, node;
let parents = [];
for (let id of args) {
node = children.find(el => el.id === id);
if (!node) {
throw new Error(`NavItem with id ${id} not found`);
}
children = node.children;
parents.push(node);
}
main = parents[parents.length - 2];
if (main.children) {
for (let item of main.children) {
示例5: updatePermission
ďťżimport { types } from 'mobx-state-tree';
export const PermissionsStoreItem = types
.model('PermissionsStoreItem', {
dashboardId: types.optional(types.number, -1),
id: types.maybe(types.number),
permission: types.number,
permissionName: types.maybe(types.string),
role: types.maybe(types.string),
team: types.optional(types.string, ''),
teamId: types.optional(types.number, 0),
userEmail: types.optional(types.string, ''),
userId: types.optional(types.number, 0),
userLogin: types.optional(types.string, ''),
inherited: types.maybe(types.boolean),
sortRank: types.maybe(types.number),
icon: types.maybe(types.string),
nameHtml: types.maybe(types.string),
sortName: types.maybe(types.string),
})
.actions(self => ({
updateRole: role => {
self.role = role;
},
updatePermission(permission: number, permissionName: string) {
self.permission = permission;
self.permissionName = permissionName;
},
}));
示例6: updatePermission
import { types } from 'mobx-state-tree';
export const PermissionsStoreItem = types
.model('PermissionsStoreItem', {
dashboardId: types.optional(types.number, -1),
permission: types.number,
permissionName: types.maybe(types.string),
role: types.maybe(types.string),
team: types.optional(types.string, ''),
teamId: types.optional(types.number, 0),
userEmail: types.optional(types.string, ''),
userId: types.optional(types.number, 0),
userLogin: types.optional(types.string, ''),
inherited: types.maybe(types.boolean),
sortRank: types.maybe(types.number),
icon: types.maybe(types.string),
name: types.maybe(types.string),
teamAvatarUrl: types.maybe(types.string),
userAvatarUrl: types.maybe(types.string),
})
.actions(self => ({
updateRole: role => {
self.role = role;
},
updatePermission(permission: number, permissionName: string) {
self.permission = permission;
self.permissionName = permissionName;
},
}));
示例7: flow
import { types, getEnv, flow } from 'mobx-state-tree';
export const Folder = types.model('Folder', {
id: types.identifier(types.number),
uid: types.string,
title: types.string,
url: types.string,
canSave: types.boolean,
hasChanged: types.boolean,
version: types.number,
});
export const FolderStore = types
.model('FolderStore', {
folder: types.maybe(Folder),
})
.actions(self => ({
load: flow(function* load(uid: string) {
// clear folder state
if (self.folder && self.folder.uid !== uid) {
self.folder = null;
}
const backendSrv = getEnv(self).backendSrv;
const res = yield backendSrv.getFolderByUid(uid);
self.folder = Folder.create({
id: res.id,
uid: res.uid,
title: res.title,
url: res.url,
canSave: res.canSave,
示例8:
import { types } from 'mobx-state-tree';
export const Language = types.model('Language', {
id: types.identifier,
name: types.maybe(types.string),
});
示例9: setName
import { types, IModelType, ISnapshottable } from "mobx-state-tree"
import { TestUserModel } from "./test-user-model"
import { IObservableArray } from "mobx"
// lol - https://github.com/Microsoft/TypeScript/issues/5938
export type __IModelType = IModelType<any, any>
export type __ISnapshottable = ISnapshottable<any>
export type __IObservableArray = IObservableArray<any>
export const TestCompanyModel = types
.model()
.props({
name: "",
employees: types.optional(types.array(TestUserModel), []),
owner: types.maybe(TestUserModel),
})
.actions(self => ({
setName(value: string) {
self.name = value
},
}))
export const createTestCompany = () =>
TestCompanyModel.create({
name: "Steve",
owner: { name: "me", age: 100 },
employees: [{ name: "a", age: 1 }, { name: "b", age: 2 }],
})
示例10: disabled
import { types } from 'mobx-state-tree';
import { CinemaReference } from '../cinemas';
import store from '../index';
import { utcToZonedTime, toDate } from 'date-fns-tz';
import { addHours } from 'date-fns';
export const Showtime = types.model('Showtime', {
id: types.identifier,
playingAt: types.Date,
cinema: types.maybe(CinemaReference),
room: types.maybe(types.string),
ticketUrl: types.maybe(types.string),
flags: types.array(types.string),
})
.preProcessSnapshot((snapshot) => {
const date = utcToZonedTime(snapshot.playingAt || 0, 'America/New_York');
return {
...snapshot,
playingAt: toDate(date, { timeZone: 'GMT' }),
};
})
.views(self => ({
get disabled() {
const timeInIceland = utcToZonedTime(Date.now(), 'GMT');
if (self.playingAt) {
return self.playingAt.getTime() < timeInIceland.getTime();
}
return false;
},
}));