Skip to content

Instantly share code, notes, and snippets.

View jamesmorgan's full-sized avatar

James Morgan jamesmorgan

View GitHub Profile
@jamesmorgan
jamesmorgan / callbackhell.js
Created August 2, 2016 12:30
callbackhell.js
fs.readdir(source, function (err, files) {
if (err) {
console.log('Error finding files: ' + err)
} else {
files.forEach(function (filename, fileIndex) {
console.log(filename)
gm(source + filename).size(function (err, values) {
if (err) {
console.log('Error identifying file size: ' + err)
} else {
@jamesmorgan
jamesmorgan / ConnectableObservable.Subscriber.ts
Created April 1, 2016 07:54
ConnectableObservable.Subscriber.ts
export class GamesComponent implements OnDestroy {
private _gamesSubscription:Subscription;
constructor(private _gamesService:GamesService){
/**
* If we had already emitted values down this observable we would immediately receive them here, without re-requesting them.
* We would be able to quickly populate the component and if need re-request the data from within the ngOnInit lifecycle hook.
*/
this._gamesSubscription = this._gamesService.gamesChanged$.subscribe((games) => {
@jamesmorgan
jamesmorgan / GamesService.ConnectableObservable.ts
Last active April 1, 2016 07:48
GamesService.ConnectableObservable.ts
@Injectable()
export class GamesService {
/** Internal model state */
private games:Object[];
/** Private Subject **/
private _gamesSource:Subject<Object[]> = new Subject<Object[]>();
/** Public Observer **/
@jamesmorgan
jamesmorgan / ElvisOperator.html
Created March 31, 2016 21:32
ElvisOperator.html
<div class="media-body">
<p>{{ competition?.description }}</p>
<p>{{ competition?.user?.name }}</p>
<p>{{ competition?.game?.title?.shortName }}</p>
</div>
@jamesmorgan
jamesmorgan / DashboardComponent.@CanActivate.ts
Last active April 3, 2016 16:29
DashboardComponent.@CanActivate.ts
/**
* This is a special hook because it is called before your component is instantiated.
* Its parameters are (next, previous) which are the components you're routing to and the component
* you've come from (or null if you have no history) respectively.
*/
@CanActivate((next, prev) => {
// No simple way to load data, how to easily inject services or how to provide the result to componenet once loaded.
// Work arounds can be found but are not ideal IMHO.
return true;
})
@jamesmorgan
jamesmorgan / DashboardComponent.OnActivate.ts
Created March 31, 2016 21:16
DashboardComponent.OnActivate.ts
export class DashboardComponent implements OnActivate {
routerOnActivate(nextInstruction:ComponentInstruction, prevInstruction:ComponentInstruction):any|Promise<any> {
// Load some data ...
}
}
@jamesmorgan
jamesmorgan / DashboardComponent.OnInit.ts
Created March 31, 2016 21:14
DashboardComponent.OnInit.ts
export class DashboardComponent implements OnInit {
ngOnInit():any {
// Load some data ...
}
}
@jamesmorgan
jamesmorgan / angular2-example-rxjs-imports.ts
Created March 20, 2016 19:19
Angular2 Example RxJs Imports
// Include the full set of RxJs features
import "rxjs/Rx";
// Add individual imports per function which is required
import 'rxjs/add/operator/share';
import 'rxjs/add/operator/map';
import 'rxjs/operator/delay';
import 'rxjs/operator/mergeMap';
import 'rxjs/operator/switchMap';
export class AdminDashboardComponent implements OnDestroy {
/** Public data */
competitions:Competition[];
/** Subscriber */
private _competitionsSubscription:Subscription;
constructor(private _competitionsService:CompetitionsService) {
// Subscribe an changes which may happen
export class CompetitionsService {
// Create a Subect to observe
private _competitionsSource:Subject<Competition[]> = new Subject<Competition[]>();
// Publicly expose the Observable - called share() to allow multiple observers
competitionsChanged$:Observable<Competition[]> = this._competitionsSource.asObservable().share();
constructor(private _http:Http) {
_http.get('http://localhost:8080/competitions')