本文整理汇总了TypeScript中mobx-state-tree.types.map方法的典型用法代码示例。如果您正苦于以下问题:TypeScript types.map方法的具体用法?TypeScript types.map怎么用?TypeScript types.map使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mobx-state-tree.types
的用法示例。
在下文中一共展示了types.map方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: all
// Dependencies
import { flow, types } from 'mobx-state-tree'
import * as api from './api'
// Model
const Item = types.model('Item', {
id: types.number,
name: types.string
})
// Store
const ItemsStore = types
.model('ItemsStore', {
_cache: types.map(Item)
})
.actions(self => {
const fetchItems = flow(function*() {
const items: Array<typeof Item.Type> = yield api.getItems()
items.forEach(item => self._cache.set(item.id.toString(), item))
})
return {
fetchItems
}
})
.views(self => ({
get all() {
return Array.from(self._cache.values())
}
}))
示例3: filteredMembers
groupId: types.identifier(types.string),
teamId: types.number,
});
type TeamGroupType = typeof TeamGroup.Type;
export interface ITeamGroup extends TeamGroupType {}
export const Team = types
.model('Team', {
id: types.identifier(types.number),
name: types.string,
avatarUrl: types.string,
email: types.string,
memberCount: types.number,
search: types.optional(types.string, ''),
members: types.optional(types.map(TeamMember), {}),
groups: types.optional(types.map(TeamGroup), {}),
})
.views(self => ({
get filteredMembers() {
let members = this.members.values();
let regex = new RegExp(self.search, 'i');
return members.filter(member => {
return regex.test(member.login) || regex.test(member.email);
});
},
}))
.actions(self => ({
setName(name: string) {
self.name = name;
},
示例4: currentUrl
import { types } from 'mobx-state-tree';
import { toJS } from 'mobx';
import { toUrlParams } from 'app/core/utils/url';
const QueryInnerValueType = types.union(types.string, types.boolean, types.number);
const QueryValueType = types.union(QueryInnerValueType, types.array(QueryInnerValueType));
export const ViewStore = types
.model({
path: types.string,
query: types.map(QueryValueType),
routeParams: types.map(QueryValueType),
})
.views(self => ({
get currentUrl() {
let path = self.path;
if (self.query.size) {
path += '?' + toUrlParams(toJS(self.query));
}
return path;
},
}))
.actions(self => {
// querystring only
function updateQuery(query: any) {
self.query.clear();
for (let key of Object.keys(query)) {
if (query[key]) {
self.query.set(key, query[key]);
}
示例5: get
id: string;
showtimes: IShowtimeFromDb[];
}
export const InTheaters = types.model('InTheaters', {
loading: false,
loaded: false,
isOffline: false,
isCache: false,
dates: types.map(types.model('DateOfMovies', {
date: types.identifier,
movies: types.map(types.model('MovieRef', {
id: types.identifier,
movie: types.reference(Movie, {
get(identifier: string) {
return Movies.movies.get(identifier) as typeof Movie.Type;
},
set(value: typeof Movie.Type) {
return value.id;
},
}),
})),
})),
})
.views(self => ({
moviesForDate(date: Date) {
const movies = self.dates.get(date.toISOString().substr(0, 10));
if (movies) {
return Array.from(movies.movies.values());
}
return [];
},
示例6: get
import { client } from '../services/graphql.service';
import { Cinema } from './models/Cinema';
import fetchCinemaByIdQuery from '../queries/fetchCinemaById.gql';
import fetchAllCinemas from '../queries/fetchAllCinemas.gql';
export const CinemaReference = types.reference(Cinema, {
get(identifier: string) {
return Cinemas.getOrLoadById(identifier) as typeof Cinema.Type || [];
},
set(value: typeof Cinema.Type) {
return value.id;
},
});
export const Cinemas = types.model('Cinemas', {
cinemas: types.map(Cinema),
})
.actions((self) => {
const addCinema = (obj: any) => {
const cinema = { ...obj };
if (self.cinemas.has(cinema.id)) {
return;
}
return self.cinemas.put(
Cinema.create(cinema),
);
};
const loadAllCinemas = flow(function* () {
示例7: currentUrl
import { types } from 'mobx-state-tree';
import { toJS } from 'mobx';
import { toUrlParams } from 'app/core/utils/url';
const QueryInnerValueType = types.union(types.string, types.boolean, types.number);
const QueryValueType = types.union(QueryInnerValueType, types.array(QueryInnerValueType));
export const ViewStore = types
.model({
path: types.string,
query: types.map(QueryValueType),
})
.views(self => ({
get currentUrl() {
let path = self.path;
if (self.query.size) {
path += '?' + toUrlParams(toJS(self.query));
}
return path;
},
}))
.actions(self => {
function updateQuery(query: any) {
self.query.clear();
for (let key of Object.keys(query)) {
self.query.set(key, query[key]);
}
}
function updatePathAndQuery(path: string, query: any) {
示例8: all
import { types } from 'mobx-state-tree'
import { values as mobxValues } from 'mobx'
const User = types.model('User', {
id: types.identifier,
nick: '',
status: ''
})
export const Users = types
.model('Users', {
items: types.optional(types.map(User), {})
})
.actions(self => {
const set = items => {
self.items = items
}
return { set }
})
.views(self => {
return {
get all() {
return mobxValues(self.items)
}
}
})
示例9: return
import { types, flow } from 'mobx-state-tree';
import { Movie } from './models/Movie';
import { client } from '../services/graphql.service';
import fetchMovieByIdQuery from '../queries/fetchMovieById.gql';
import fetchNewMoviesQuery from '../queries/fetchNewMovies.gql';
import { addDays } from 'date-fns';
import { mapMovie } from 'utils/mapMovie';
import { FetchPolicy } from 'apollo-boost';
export const Movies = types.model('Movies', {
movies: types.map(Movie),
comingSoon: types.array(types.reference(Movie)),
isOffline: false,
isCache: false,
})
.actions(self => ({
addPartialMovie: (obj: any) => {
return (self as any).addMovie(obj, true);
},
addMovie: (obj: any, isPartial: boolean) => {
if (self.movies.has(obj.id)) {
const movie = self.movies.get(obj.id);
return movie!.update(obj, isPartial);
}
return self.movies.put(
Movie.create(mapMovie(obj, isPartial)),
);
},
loadMovieById: flow(function* loadMovieById(movieId: string) {
const start = new Date();
示例10: get
import { client } from '../services/graphql.service';
import { Genre } from './models/Genre';
import fetchGenreByIdQuery from '../queries/fetchGenreById.gql';
import fetchAllGenres from '../queries/fetchAllGenres.gql';
export const GenreReference = types.reference(Genre, {
get(identifier: string) {
return Genres.getOrLoadById(identifier) as typeof Genre.Type || [];
},
set(value: typeof Genre.Type) {
return value.id;
},
});
export const Genres = types.model('Genres', {
genres: types.map(Genre),
})
.actions((self) => {
const addGenre = (obj: any) => {
const genre = { ...obj };
if (self.genres.has(genre.id)) {
return;
}
return self.genres.put(
Genre.create(genre),
);
};
const loadAllGenres = flow(function* () {