Skip to content

Instantly share code, notes, and snippets.

View vivainio's full-sized avatar

Ville Vainio vivainio

View GitHub Profile

Simple approach to ng1

  • Put all your "important" state (that can't be trivially restored) in "root" services. Controllers should have no valuable state
    • i.e. you can tear down a directive completely without losing any data
    • antipattern: communicating across controllers outside shallow on-foo event
  • Use the new angular.component() for everything. You can polyfill it before ng 1.5.
  • Create small components (i.e. small enough that html can be inlined). Controller and html are in the same file

Bonus:

@vivainio
vivainio / dedux.ts
Last active January 14, 2016 09:44
// dedux: the demented redux
// like redux in:
// - one tree stores all the application data
// - all changes happen through single function (so easy to debug/trace)
// not like redux in:
// - can use mutation freely (of course "time travel" will be harder if you don't)
// - no reducers. just do your stuff in the function passed to do()
class TestComponentController {
foo: string;
constructor() {
console.log('ctor', this.foo);
}
}
(<any>angular).module('app')
.component('testcomponent', {
@vivainio
vivainio / suave_json.fs
Created December 19, 2015 17:04
Minimal Suave setup that returns json objects
// depends on NewtonSoft.Json
let JSON v =
let jsonSerializerSettings = new JsonSerializerSettings()
jsonSerializerSettings.ContractResolver <- new CamelCasePropertyNamesContractResolver()
JsonConvert.SerializeObject(v, jsonSerializerSettings)
type Task = {
name: string
}

Before discovering Python (the most fun way to program!) in 1999, I studied various obscure programming languages:

Haskell, OCaml - functional languages, mathematically pleasing but not very practical for normal applications. Erlang is also in this family, but has core support for concurrency. And I hear it has even been used in real world!

Prolog - "Logical"/AI programming, for deductive problem solving. Never found any use for this.

Lisp, Scheme (still use it in the form of emacs lisp) - actually a very useful family of languages. A bit like Python, but with an awkward syntax. If Python (and Ruby) suddenly disappeared from the world, Lisp would be the best contender for the language supremacy. People often refer these languages as functional languages, but they are not. That's probably why they are used in production applications.

Ruby - A rip-off of Python. It's big in Japan, apparently. The Ruby crowd likes to tout how much better it is than Perl, while they really should be comparing it to Python. An

@vivainio
vivainio / nges6service.js
Created January 18, 2015 17:16
Convenient way to use ES6 classes as angular service
/* Convenient way to use ES6 classes as service, with DI
If you use service(), you will need to capture injectables in ctor, like
constructor($http) {
this.http = $http;
}
If you use factory, you can just keep the injectables in the outer factory function args
# edit this by: notepad $PROFILE
# assumes you have Github for Windows installed (or just posh-git may be enough?)
# Load posh-git example profile
. 'C:\Users\villevai\Documents\WindowsPowerShell\Modules\posh-git\profile.example.ps1'
# Set up a simple prompt, adding the git prompt parts inside git repos
function global:prompt {
$realLASTEXITCODE = $LASTEXITCODE
# Reset color, which can be messed up by Enable-GitColors
+ CategoryInfo : WriteError: (test-max-data-size.js:FileInfo) [Copy-Item], PathTooLongException
+ FullyQualifiedErrorId : CopyDirectoryInfoItemIOError,Microsoft.PowerShell.Commands.CopyItemCommand
cp : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.
At line:1 char:1
$folder = "Bogus"
$myGroup = "LAB\Workgroup1"
$acl = Get-Acl $folder
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule("$myGroup", "ReadData", "ContainerInherit, ObjectInherit", "None", "Allow")
$acl.AddAccessRule($rule)
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule("$myGroup", "CreateFiles", "ContainerInherit, ObjectInherit", "None", "Allow")
$acl.AddAccessRule($rule)
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule("$myGroup", "AppendData", "ContainerInherit, ObjectInherit", "None", "Allow")
$acl.AddAccessRule($rule)
Set-Acl $folder $acl
interface ComponentEntry {
t?: string;
repo?: string;
};
...
var cont = fs.readFileSync("test.yaml").toString();
var o = yaml.load(cont);