Thursday, July 24, 2014

On TypeScript, the this keyword doesn't always reference the instance of a class

///<reference path='jquery.d.ts' />

class Mate {
    public n : number;

    public name : string = "Freddie";

    public nick : string = "Fred";

    public message() : void {

        var people = new Array<Person>();

            var p = new Person();
   = "Ely";

            var p = new Person();
   = "Raymund";

        $.each(people, function() {
            alert('inline: ' + + ' -- ' + this.nick);
        inline: Ely -- undefined
        inline: Raymund -- undefined

        $.each(people, this.alerter);
        Coming from traditional OOP, I expected:
        Freddie -- Fred
        Freddie -- Fred

        What happens:
        Ely -- undefined
        Raymund -- undefined

        $.each(people, () => alert('inline: ' + + ' -- ' + this.nick) );
        inline: Freddie -- Fred
        inline: Freddie -- Fred

        $.each(people, () => this.alerter());
        Freddie -- Fred
        Freddie -- Fred


    public alerter() : void {
        alert( + ' -- ' + this.nick);



class Person {
    public name : string;

$(function() {
    var m = new Mate();

Thursday, May 15, 2014

Tuesday, May 6, 2014

PostgreSQL (mis)feature

Martin Smith pointed out a gotcha on Postgres feature I recommended

GROUP BY on alias doesn't work properly when the alias has a same name from the table's columns

select 'foo' as Table_name
from information_schema.tables 
group by Table_name

Live code:!15/d41d8/1920

Tuesday, April 22, 2014

Dropdown in ng-grid

The controller:

var app = angular.module('app', ['ngGrid']);

app.controller('bodyController', function($scope, LanguageList) {
  $scope.myData = [{name: "Moroni", language: 'fr'},
                     {name: "Tiancum", language: 'en'},
                     {name: "Enos", language: 'nl'}];
    $scope.gridOptions = {
            data: 'myData',
                enableCellSelection: true,
                enableRowSelection: false,
                enableCellEditOnFocus: true,
            columnDefs: [
                    field: 'language', enableFocusedCellEdit: true,
                    editableCellTemplate: 'language_edit.html',
                    cellFilter : 'LanguageMap'
                {field: 'name', enableFocusedCellEdit: true }

            canSelectRows: false
    $scope.languages = LanguageList;
.factory('LanguageList', function() {
  return [
      { language_code : 'en', language_title: 'English' },
      { language_code : 'fr', language_title: 'French' },
      { language_code : 'nl', language_title: 'Netherlands' }
.filter('LanguageMap', function(LanguageList) {
  return function(input) {
    var languagesMatched = LanguageList.filter(function(lang) {
      return lang.language_code == input;
    if (languagesMatched.length == 1)
        return languagesMatched[0].language_title;
        return '*unknown language*'
    return text;

Main page
<!DOCTYPE html>
<html ng-app="app">
  <head lang="en">
    <meta charset="utf-8">
    <title>Custom Plunker</title>
    <script type="text/javascript" src=""></script>
    <script src="//"></script>
    <script type="text/javascript" src="ng-grid.js"></script>
    <link rel="stylesheet" href="style.css">
    <link rel="stylesheet" href="ng-grid.css">
      document.write('<base href="' + document.location + '" />');
    <script src="app.js"></script>
<body ng-controller="bodyController">
    <div ng-grid="gridOptions" style="height: 400px">


Dropdown template:

ng-options='l.language_code as l.language_title for l in languages'

ng-class="'colt' + $index" ng-model="COL_FIELD" ng-input='COL_FIELD' data-placeholder="-- Select One --" 


Live code:

Friday, March 14, 2014

Sprite Kit: Animation in English-Speak

Message sequence of actions to SKAction, then message that sequence of actions to repeatActionForever, then run that forever action to the player (SKSpriteNode)

Thursday, March 13, 2014

Sprite Kit: Up is down, down is up

There's no error on messaging the location.x to moveToX action of SKAction, think spaceship which just move left and right and remains stationary on its Y

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    UITouch* touched = [touches anyObject];

    CGPoint location = [touched locationInView:self.view];

    SKAction* moveAction = [SKAction moveToX: location.x duration:0.0];
    [player runAction: moveAction];

However, it's preferable to use locationInNode. If we want the spaceship to move in all direction yet we use locationInView, the app will exhibit this same error:

CGPoint location = [touched locationInView:self.view]
SKAction* moveAction = [SKAction moveTo: location duration:0.0];    

Though the spaceship's X will move accordingly to your touch, the spaceship's Y will not follow your finger movements.

To rectify that flaw, get the location from SKScene's view (self) instead of the UIView's view (self.view), then message that view to touched object's locationInNode instead of locationInView

CGPoint location = [touched locationInNode:self];
SKAction* moveAction = [SKAction moveTo: location duration:0.0];

Happy Coding! ツ

Wednesday, March 12, 2014

Sprite Kit: Determine touch location in English-speak

Create a touchesMoved event in the game scene. To determine the touched location, first get a UITouch from touched object (use anyObject), then from that UITouch object get its location(CGPoint) in view, with the parameter being the game scene's view.

To move an object (e.g. player (SKSpriteNode)), message the location(CGPoint) to the moving action functionality (moveTo) of an action(SKAction). Then message that action to the player's (SKSpriteNode) run action functionality