本文整理汇总了TypeScript中@ngrx/entity.createEntityAdapter函数的典型用法代码示例。如果您正苦于以下问题:TypeScript createEntityAdapter函数的具体用法?TypeScript createEntityAdapter怎么用?TypeScript createEntityAdapter使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了createEntityAdapter函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: Error
export function createEntityDefinition<T, S extends object>(metadata: EntityMetadata<T, S>): EntityDefinition<T> {
let entityName = metadata.entityName;
if (!entityName) {
throw new Error('Missing required entityName');
}
metadata.entityName = entityName = entityName.trim();
const selectId = metadata.selectId || defaultSelectId;
const sortComparer = (metadata.sortComparer = metadata.sortComparer || false);
const entityAdapter = createEntityAdapter<T>({ selectId, sortComparer });
const entityDispatcherOptions: Partial<EntityDispatcherDefaultOptions> = metadata.entityDispatcherOptions || {};
const initialState: EntityCollection<T> = entityAdapter.getInitialState({
entityName,
filter: '',
loaded: false,
loading: false,
changeState: {},
...(metadata.additionalCollectionState || {})
});
const noChangeTracking = metadata.noChangeTracking === true; // false by default
return {
entityName,
entityAdapter,
entityDispatcherOptions,
initialState,
metadata,
noChangeTracking,
selectId,
sortComparer
};
}
示例2: reducer
import { PanelActions, PanelActionTypes } from '../actions/panel.actions';
import { PanelDetails, PanelListItem } from '../../shared/models';
export interface PanelState extends EntityState<PanelListItem> {
// entities: { [id: number]: PanelListItem };
panelsLoaded: boolean;
panelsLoading: boolean;
panelDetails: PanelDetails;
panelDetailsLoading: boolean;
panelDetailsLoaded: boolean;
}
export const adapter: EntityAdapter<PanelListItem> = createEntityAdapter<
PanelListItem
>();
export const initialState: PanelState = adapter.getInitialState({
//entities: {},
panelsLoaded: false,
panelsLoading: false,
panelDetails: null,
panelDetailsLoaded: false,
panelDetailsLoading: false
});
export function reducer(
state = initialState,
action: PanelActions
): PanelState {
示例3: reducer
import { createEntityAdapter, EntityAdapter, EntityState } from '@ngrx/entity';
import * as fromOrganizations from '../actions/organization.actions';
import { Organization } from '../../shared/models';
export interface OrganizationState extends EntityState<Organization> {
entities: { [id: number]: Organization };
organizationsLoading: boolean;
organizationsLoaded: boolean;
}
export const adapter: EntityAdapter<Organization> = createEntityAdapter<
Organization
>();
export const initialState: OrganizationState = adapter.getInitialState({
entities: {},
organizationsLoading: false,
organizationsLoaded: false
});
export function reducer(
state = initialState,
action: fromOrganizations.OrganizationActions
): OrganizationState {
switch (action.type) {
case fromOrganizations.OrganizationActionTypes.LoadOrganizations: {
return {
...state,
organizationsLoading: true
示例4: switch
import { createEntityAdapter } from '@ngrx/entity';
import { State } from '@ngrx/store';
import { ActionReducer } from '@ngrx/store';
import * as LabelActions from './../actions/label.actions';
import { LabelUI } from './../models/label.model';
import { initialState, LabelState } from './../states/label.state';
export type Action = LabelActions.All;
const labelAdapter = createEntityAdapter<LabelUI>();
export const LabelReducer: ActionReducer<LabelState> = (state = initialState, action: Action) => {
switch (action.type) {
case LabelActions.GET_SUCCESS: {
return labelAdapter.addAll(action.payload, state);
}
case LabelActions.GET_ERROR: {
return state;
}
case LabelActions.ADD_SUCCESS: {
return labelAdapter.addOne(action.payload, state);
}
case LabelActions.ADD_ERROR: {
return state;
}
default: {
return state;
}
}
};
示例5: reducer
import { EntityState, EntityAdapter, createEntityAdapter } from '@ngrx/entity';
import { Article, Page } from '../../shared/models';
import { ArticlesActions, ArticlesActionTypes } from '../actions/articles';
export interface State extends EntityState<Article> {
selectedArticleId: string | null;
selectedArticle: Article | null;
page: Page | null;
}
export const adapter: EntityAdapter<Article> = createEntityAdapter<Article>({
selectId: (article: Article) => article.id,
sortComparer: false,
});
export const initialState: State = adapter.getInitialState({
selectedArticleId: null,
selectedArticle: null,
page: {
pageSize: 8,
pageIndex: 0,
startIndex: 0,
endIndex: 0
} as Page
});
export function reducer(
state = initialState,
action: ArticlesActions
): State {
示例6: reducer
import { createEntityAdapter, EntityAdapter, EntityState } from '@ngrx/entity';
import { WidgetActions, WidgetActionTypes } from './widget.actions';
import { Widget } from './widget.model';
export interface State extends EntityState<Widget> {
// additional entities state properties
loaded: boolean;
loading: boolean;
selectedWidgetId: string;
}
export const adapter: EntityAdapter<Widget> = createEntityAdapter<Widget>();
export const initialState: State = adapter.getInitialState({
// additional entity state properties
loaded: false,
loading: false,
selectedWidgetId: '',
});
export function reducer(state = initialState, action: WidgetActions): State {
switch (action.type) {
case WidgetActionTypes.DATABASE_LISTEN_FOR_DATA_STOP: {
return adapter.removeAll({
...state,
loaded: false,
loading: false,
selectedWidgetId: '',
});
}
示例7: reducer
EntityState,
Update
} from '@ngrx/entity';
import { Hero } from '../../models/hero';
import { HeroActions, HeroActionTypes } from '../actions/hero.actions';
export interface State extends EntityState<Hero> {
loaded: boolean;
loading: boolean;
error: any;
selectedHeroId: number;
searchHeroes: Hero[];
}
export const adapter: EntityAdapter<Hero> = createEntityAdapter<Hero>();
export const initialState: State = adapter.getInitialState({
loaded: false,
loading: false,
selectedHeroId: null,
error: null,
searchTerm: '',
searchHeroes: null
});
export function reducer(state = initialState, action: HeroActions): State {
switch (action.type) {
case HeroActionTypes.heroGetHeroes:
case HeroActionTypes.heroAddHero:
case HeroActionTypes.heroDeleteHero:
示例8: sortByCourseAndSeqNo
loading:boolean;
}
function sortByCourseAndSeqNo(l1: Lesson, l2:Lesson) {
const compare = l1.courseId - l2.courseId;
if (compare != 0) {
return compare;
}
else {
return l1.seqNo - l2.seqNo;
}
}
export const adapter : EntityAdapter<Lesson> =
createEntityAdapter<Lesson>({
sortComparer: sortByCourseAndSeqNo
});
const initialLessonsState = adapter.getInitialState({
loading: false
});
export function lessonsReducer(state = initialLessonsState,
action: CourseActions): LessonsState {
switch(action.type) {
case CourseActionTypes.LessonsPageCancelled:
示例9: coursesReducer
import {Course} from './model/course';
import {createEntityAdapter, EntityAdapter, EntityState} from '@ngrx/entity';
import {CourseActions, CourseActionTypes} from './course.actions';
export interface CoursesState extends EntityState<Course> {
allCoursesLoaded:boolean;
}
export const adapter : EntityAdapter<Course> =
createEntityAdapter<Course>();
export const initialCoursesState: CoursesState = adapter.getInitialState({
allCoursesLoaded: false
});
export function coursesReducer(state = initialCoursesState , action: CourseActions): CoursesState {
switch(action.type) {
case CourseActionTypes.CourseLoaded:
return adapter.addOne(action.payload.course, state);
case CourseActionTypes.AllCoursesLoaded:
示例10: reducer
import {
PlaylistActionTypes,
PlaylistsAction
} from '../actions/playlist.actions';
import { VideoActions, VideoActionTypes } from '../actions/video.actions';
export interface PlaylistState extends EntityState<Playlist> {
playlistsLoaded: boolean;
playlistsLoading: boolean;
playlistDetails: PlaylistDetails;
playlistDetailsLoaded: boolean;
playlistDetailsLoading: boolean;
}
export const adapter: EntityAdapter<Playlist> = createEntityAdapter<Playlist>();
export const initialState: PlaylistState = adapter.getInitialState({
playlistsLoaded: false,
playlistsLoading: false,
playlistDetails: null,
playlistDetailsLoaded: false,
playlistDetailsLoading: false
});
export function reducer(
state = initialState,
action: PlaylistsAction | VideoActions
): PlaylistState {
switch (action.type) {
case PlaylistActionTypes.LoadPlaylists: {