Skip to content

Instantly share code, notes, and snippets.

View roelofjan-elsinga's full-sized avatar
Building fast applications

Roelof Jan Elsinga roelofjan-elsinga

Building fast applications
View GitHub Profile
@roelofjan-elsinga
roelofjan-elsinga / redux.config.ts
Created July 2, 2018 18:32
Initialize ng-redux in AngularJS with an existing store
import * as angular from 'angular';
import ngRedux from 'ng-redux';
import {store} from "../../app/redux";
angular.module('config.redux', [ngRedux])
.config(['$ngReduxProvider', $ngReduxProvider => {
$ngReduxProvider.provideStore(store);
@roelofjan-elsinga
roelofjan-elsinga / app.module.ts
Last active July 2, 2018 18:42
Initialize ng2-redux in Angular
import {NgModule} from '@angular/core';
import {AppComponent} from './app.component';
import {NgRedux, NgReduxModule} from '@angular-redux/store';
import {store} from "./redux";
import {IAppState} from "./redux/interfaces/state";
@NgModule({
declarations: [
AppComponent
],
@roelofjan-elsinga
roelofjan-elsinga / actions.ts
Created July 3, 2018 19:00
Example action in Redux
export const ADD_LANGUAGE = 'ADD_LANGUAGE';
@roelofjan-elsinga
roelofjan-elsinga / reducers.ts
Created July 3, 2018 19:04
Example reducers in Redux
import {combineReducers} from "redux";
import {language} from "./languages";
export const rootReducer = combineReducers({
languages
});
@roelofjan-elsinga
roelofjan-elsinga / languages.ts
Created July 3, 2018 19:06
Example languages reducer in Redux
import {ADD_LANGUAGE} from "../actions";
import {INITIAL_STATE} from "./initial-state";
import {ILanguage} from "../interfaces/languages";
export function languages(state = INITIAL_STATE.languages, action):ILanguage[] {
switch (action.type) {
case ADD_LANGUAGE:
return state.concat(Object.assign({}, action.language));
}
@roelofjan-elsinga
roelofjan-elsinga / index.ts
Created July 3, 2018 19:19
Example store in Redux
import {createStore} from 'redux';
import {rootReducer} from "./reducers";
import {INITIAL_STATE} from "./reducers/initial-state";
export const store = createStore(rootReducer, INITIAL_STATE);
@roelofjan-elsinga
roelofjan-elsinga / initial-state.ts
Created July 3, 2018 19:23
The example initial state in Redux
import {IAppState} from "../interfaces/state";
export const INITIAL_STATE: IAppState = {
languages: []
};
@roelofjan-elsinga
roelofjan-elsinga / state.ts
Created July 3, 2018 19:26
Example IAppState
import {ILanguage} from "./languages";
export interface IAppState {
languages: ILanguage[];
}
@roelofjan-elsinga
roelofjan-elsinga / angularjs.ts
Created July 3, 2018 19:35
Dispatch an action with AngularJS in Redux
import * as angular from 'angular';
import ngRedux from 'ng-redux';
import {ADD_LANGUAGE} from "../../../app/redux/actions";
angular.module("any.module", [ngRedux])
.run(['$ngRedux', $ngRedux => {
let language = {
shortcode: 'en',
@roelofjan-elsinga
roelofjan-elsinga / any.component.ts
Created July 3, 2018 19:49
Subscribing to an action in Angular 6
import {Component, OnInit} from '@angular/core';
import {NgRedux, select} from "@angular-redux/store";
import {IAppState} from "./path/to/state";
@Component({
selector: 'app-any',
templateUrl: './any.component.html',
styleUrls: ['./any.component.scss']
})
export class AnyComponent implements OnInit {