本文整理汇总了TypeScript中mobx-state-tree.types.reference方法的典型用法代码示例。如果您正苦于以下问题:TypeScript types.reference方法的具体用法?TypeScript types.reference怎么用?TypeScript types.reference使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mobx-state-tree.types
的用法示例。
在下文中一共展示了types.reference方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: get
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 [];
},
}))
示例2: 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();
示例3: get
import { types, flow } from 'mobx-state-tree';
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),
);
};
示例4: get
import { types, flow } from 'mobx-state-tree';
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),
);
};