Skip to content

Instantly share code, notes, and snippets.

View carmichaelize's full-sized avatar

Scott Carmichael carmichaelize

View GitHub Profile
@carmichaelize
carmichaelize / foo-component.jsx
Created November 3, 2017 08:55
React — Importing Global Variables from the Window Object
import React, { Component } from 'react';
import Foo from './foo-service';
class FooComponent extends Component {
foo = Foo;
render() {
return (<div>{this.foo.name}</div>)
}
}
@carmichaelize
carmichaelize / foo-bar.jsx
Last active July 19, 2017 20:07
Shallow Rendering a Redux Component in React Tests
import React, { Component } from 'react';
import { connect } from 'react-redux';
export class FooBar extends Component {
greet() {
return `Hello ${store.foo} ${store.bar}!`;
}
render() {
@carmichaelize
carmichaelize / is-online.component.ts
Created February 12, 2017 14:12
Testing for an internet connection.
import { Component } from '@angular/core';
@Component({
template: `
<h1>Is Online: {{ isOnline }}</h1>
<span (click)="clickHandler()">Click Me</span>
`,
})
export class MyComponent {
private baseUrl = 'http://www.test.com';
@carmichaelize
carmichaelize / child-1.component.ts
Last active September 7, 2018 08:15
Angular2 component factory resolver example
import { Component } from '@angular/core';
import { ChildBaseComponent } from './child-base.component';
@Component({
template: `This is Child Component 1`
})
export class Child1Component extends ChildBaseComponent {
constructor(){
//Run base constructor
@carmichaelize
carmichaelize / capitalize.pipe.ts
Last active August 21, 2016 20:36
Angular2 string capitalisation pipe
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'capitalize'
})
export class Capitalize implements PipeTransform {
transform(str: string, all: boolean) {
//Capitalize all the words
if (all) {
@carmichaelize
carmichaelize / angularjs-$compile.js
Last active July 18, 2018 09:30
AngularJS $compile example
function MyDirective ($templateRequest, $compile) {
var templateBase = '/directives/my_directive/views/';
var childTemplates = {
child1: 'child1.html',
child2: 'child2.html'
};
return {
@carmichaelize
carmichaelize / angular-2-component-resolver.ts
Last active July 28, 2016 20:17
Angular2 component resolver example
import {Component, Input, ViewChild, ViewContainerRef, ComponentResolver, ComponentFactory} from '@angular/core';
import {Child1Component} from './child1.component';
import {Child2Component} from './child2.component';
@Component({
selector: 'parent',
template: `
<div #target></div>
`
})
// Here You can type your custom JavaScript...
jQuery.fn.highlight = function (str, className) {
var regex = new RegExp(str, "gi");
return this.each(function () {
$(this).contents().filter(function() {
return this.nodeType == 3 && regex.test(this.nodeValue);
}).replaceWith(function() {
return (this.nodeValue || "").replace(regex, function(match) {
return "<span class=\"" + className + "\">" + match + "</span>";
});
@carmichaelize
carmichaelize / transclusion.js
Created November 8, 2015 13:45
Custom Angular directive transclusion functions
//Shared scope
function myDirective() {
return {
restrict: 'EA',
templateUrl: 'my-directive.html',
transclude: true,
scope: false,
controllerAs: 'ctrl',
link: function(scope, element, attrs, ctrl, transclude) {
transclude(scope, function(clone, scope) {
@carmichaelize
carmichaelize / $dirtyCheck.js
Last active August 29, 2015 14:24
Check $dirty/$prestine state of an angular model.
(function () {
'use strict';
function $dirtyCheck(){
var watchers = [],
dirty = false;
return {
add: function($scope, model){
if ($scope && model){
var watcher = $scope.$watch(model, function(isDirty) {