Skip to content

Instantly share code, notes, and snippets.

View brakmic's full-sized avatar
🏠

Harris Brakmić brakmic

🏠
View GitHub Profile
@brakmic
brakmic / append_script_method.ts
Created March 12, 2017 14:00
helper method for script insertion
private appendScript(host: HTMLElement, data: string, index: number) {
this.ngZone.runOutsideAngular(() => {
const script = document.createElement('script');
script.type = 'text/javascript';
script.charset = 'utf-8';
script.id = this.vrScriptPrefix + index;
script.defer = true;
script.async = true;
script.text = data;
host.appendChild(script);
@brakmic
brakmic / vr_module_interface.ts
Created March 14, 2017 09:54
IVrModule interface
interface IVrModule {
id: string;
name: string;
type: VrModuleType;
markup: string;
scripts?: string[];
}
@brakmic
brakmic / vr_module_reducer.ts
Created March 14, 2017 14:56
vr module reducer
const vrModuleReducer: ActionReducer<IVrModule[]> = (state: IVrModule[] =
initialState, action: Action) => {
switch (action.type) {
case VR_MODULE_REMOVED:
return _.filter(state, (mod) => mod.id === action.payload.id);
case VR_MODULE_ADDED:
return _.concat(state, action.payload);
default:
return state;
}
@brakmic
brakmic / state_management_with_redux.ts
Last active March 14, 2017 15:19
state management
/**
* Initialize subscriptions and get app state via @ngrx (Redux implementation for Angular2)
*/
public ngOnInit() {
[....snip...]
this.availableModules = <Observable<IVrModule[]>> this.store.select('vrModule');
[....snip...]
}
@brakmic
brakmic / provide_store.ts
Created March 14, 2017 15:15
provide a reducer store
const appStore = provideStore(
{
vrModule: vrModuleReducer
},
{
vrModule: this.vrModule
}
);
@brakmic
brakmic / declare_actions.ts
Created April 11, 2017 07:52
declare a set of actions
export interface ICustomerActions {
INIT: string;
INIT_FAILED: string;
INITIALIZED: string;
SELECTED: string;
CHANGED: string;
SAVED: string;
DELETED: string;
}
@brakmic
brakmic / define_action_classes.ts
Last active April 11, 2017 08:10
define action classes
export class InitCustomerAction implements Action {
type = CustomerActionTypes.INIT;
payload: ICustomer = null;
}
export class InitializedCustomerAction implements Action {
type = CustomerActionTypes.INITIALIZED;
constructor(public payload: ICustomer) { }
}
@brakmic
brakmic / define_union_type_for_actions.ts
Created April 11, 2017 08:16
group thematically related actions together
export type CustomerAction
= InitCustomerAction
| InitializedCustomerAction
| InitFailedCustomerAction
| CustomerSelectedAction
| CustomerChangedAction
| CustomerDeletedAction
| CustomerSavedAction;
@brakmic
brakmic / customer_action_creator.ts
Created April 11, 2017 08:41
action creator for customer actions
import { Injectable } from '@angular/core';
import { Action } from '@ngrx/store';
import { ICustomer } from '../../interfaces';
import * as customer from '../customer.actions';
@Injectable()
export class CustomerActions {
public customerInit() {
return new customer.InitCustomerAction();
}
@brakmic
brakmic / customer_reducer.ts
Created April 11, 2017 09:07
reducer for handling customer actions
import { ICustomerState, initialCustomerState } from '../states/sub-states';
import { CustomerAction, CustomerActionTypes } from '../actions';
export function customerReducer(
state: ICustomerState = initialCustomerState,
action: CustomerAction
): ICustomerState {
switch (action.type) {
case CustomerActionTypes.INITIALIZED:
return (<any>Object).assign({}, state, {