Skip to content

Instantly share code, notes, and snippets.

View jens1101's full-sized avatar

Jens Tschirpig jens1101

View GitHub Profile
@jens1101
jens1101 / testVar.js
Created November 21, 2016 07:09
Easily check if a variable is null or undefined
function isNullOrUndefined(variable) {
// We use '==' instead of '===', because it will automatically cast the varaible to null
// if it is undefined. There are no other side-effects, so this is one of the few cases
// where the use of '==' is justified.
return variable == null;
}
@jens1101
jens1101 / ngModelAndChangeDemo.js
Last active November 3, 2016 15:45
Demonstrates how you can use ngModel and ngChange in Angular 1.5 components
angular.module('demo', [])
.component('inputDemo', {
require: {
parent: '^ngModel'
},
template: '<input ng-model="$ctrl.text" /><button ng-click="$ctrl.update()">change</button>',
controller: function($timeout) {
var $ctrl = this;
$ctrl.text;
@jens1101
jens1101 / controller.php
Created October 20, 2016 07:15
Posting data to Codeigniter via pure Angularjs
<?php
class User
{
public function save()
{
$username = $this->input->post('name');
$userData = json_decode($this->input->post('data'));
$doStuff($username, $userData);
}
@jens1101
jens1101 / app.js
Created October 18, 2016 08:56
Angularjs basic transclusion with require
angular.module('testApp', [])
.component('parent', {
template: '<h2>{{$ctrl.title}}</h2><div ng-transclude></div>',
transclude: true,
controller: function() {
this.title = 'Foobar'
}
})
.component('child', {
require: {
@jens1101
jens1101 / index.html
Created October 11, 2016 11:48
Set iframe contents via jQuery
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<iframe></iframe>
@jens1101
jens1101 / arrayCopy-es5.js
Created July 28, 2016 09:03
Copy contents of one array into another without breaking references in JS
function arrayCopy(to, from) {
Array.prototype.push.apply(to, from);
}
@jens1101
jens1101 / gzip.cs
Created July 28, 2016 08:46
Adding files to Gzip in C#
public static void Compress(FileInfo fileToCompress)
{
var bytes = File.ReadAllBytes(fileToCompress.FullName);
using (FileStream fs = new FileStream(fileToCompress.FullName + ".gzip", FileMode.CreateNew))
using (GZipStream zipStream = new GZipStream(fs, CompressionMode.Compress, false))
{
zipStream.Write(bytes, 0, bytes.Length);
}
}
@jens1101
jens1101 / SomeController.cs
Created July 13, 2016 15:15
ASP.NET MVC return custom response
if(fileToImport.ContentLength > 5 * 1024 * 1024)
{
Response.StatusCode = 500;
Response.Write("File is too large to import! The max file size is 5MB.");
Response.StatusDescription = "Internal server error";
return;
}
@jens1101
jens1101 / angular-upload.js
Last active May 22, 2018 09:04
Upload files via AJAX
angular.module('uploadFile', []).service('uploadService', ['$http', function ($http) {
this.uploadFile = function () {
var field = document.createElement('input')
field.setAttribute('type', 'file')
field.setAttribute('accept', '.json, application/json')
field.onchange = function () {
var data = new FormData()
data.append('connectionId', 'banana')
data.append('fileToImport', field.files[0])
@jens1101
jens1101 / AddFile.cs
Last active July 4, 2016 11:31
Adding files to ZIP in C#
function saveFileAsZip(string filePath) {
using (ZipFile zip = new ZipFile())
{
//Add string
zip.AddEntry("dataset.json", "{ foo: 'bar' }");
//Add file as byte array
zip.AddEntry("customName.json", System.IO.File.ReadAllBytes(filePath));
//Add file, preserving path
zip.AddFile(filePath);