Jan 15, 2017

Apply Mixin Pattern for AngularJs applications

We love single page applications with AngularJs. We always aim to create high performance apps with beautiful source code. A developer needs to write readable and maintainable code. Some features to evaluate: class hierarchies, directory hierarchies, design patterns.

Example of good directory hierarchy:

We never desire a file with too much lines. On our projects, a source file does not exceed 600 lines. We always split a large file into many small files. This habit is good to write maintainable code. A technique used to split file is applying the Mixin pattern.

The $controller service is responsible for instantiating controllers, so we use it to create the "inheritance" with angular.extend(). Every our Angular controllers initialize the authentication controller (named AuthController) as required. Every line of the the authentication controller embed to run first. We think this is a good design pattern.


var AuthController = function($auth, $scope, $http, $warning, $state, $stateParams) {
    $scope.isAuthenticated = function() {
        return $auth.isAuthenticated();
    };

    $scope.logout = function() {
        if (!$auth.isAuthenticated());
        $auth.logout();
    };

    $scope.login = function() {
        var credentials = {
            email: $scope.email,
            password: $scope.password
        };

        $auth
            .login(credentials)
            .then(function(res) {
                $auth.setToken(res);
                var loginModal = angular.element('#m_login');
                if (loginModal.hasClass('in')) {
                    loginModal.modal('toggle');
                }
                if ($stateParams.next) {
                    // logged in, redirect to next state
                    $state.go($stateParams.next.name);
                }
            })
            .catch(function(res) {
                $warning(res);
            });
    };
}

And other controllers extend from the authentication controller. For example:

var EntryCollection = function($scope, $http, $controller, $rootScope, $warning) {
    angular.extend(this, $controller('AuthController', {$scope: $scope}));
    angular.extend(this, $controller('TagCollection', {$scope: $scope}));

    $scope.page_current = 1;
    $scope.paginate = function(page_index) {
        $http({
            url: 'api/entry?page=' + page_index,
            method: 'GET'
        }).then(function(res) {
            $scope.entries = res.data.data;
            $scope.page_last = res.data.last_page;
            // save current page
            $scope.page_current = page_index;
        }, function(res) {
            $warning(res);
        });
    };
    // start paginating
    $scope.paginate($scope.page_current);
};

Dec 19, 2016

How to build Gradle without running tests?

Build afsj by
gradle build
3 year ago when I code afsj the unit tests passed, but now
To build Gradle without running unit tests:

$ gradle build -x test

x means exclude.

How to install Composer, PHPUnit in Ubuntu

Composer and PHPUnit packed with phar, so we need only append these lines into ~/.bashrc:

# change location to your phpunit.phar
alias composer=~/bin/composer/composer.phar
alias phpunit=~/bin/phpunit/phpunit.phar
We also install composer this way:

sudo curl -sS http://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
Check them installed or not?
phpunit --version
composer --version

Dec 13, 2016

ConTEXT - a lightweight editor for developer Delphi and FreePascal

ConText is a freeware text editor (on Windows) written in Delphi (compiled with using Borland Delphi). Go to the home page here
See the following configuration:

Using ConTEXT as text editor and FreePascal as compiler we have a cool "IDE"! :D
We configured compiling source code by F9 and executing by F10. I wrote a related blog here (in Vietnamese).

Dec 6, 2016

Data structures in Python and PHP

Remember that PHP use the array language for all list, hash, set, tuple... types.

1. List

In Python, to create a list, we put a number of expressions in square brackets. In PHP we usually use the array language construct.
For example the Python code snippet:

a = ['Python', 'PHP']
is equivalent to following PHP

$a = array('Python', 'PHP');
Note that PHP 5.3 introduces declaring PHP list with using square bracket syntax:

$a = ['Python', 'PHP'];

2. Hastable

Some times called associative arrays, dictionaries, or maps. A hash is an un-ordered group of key-value pairs. The keys are usually unique strings.

In Python we use brace bracket to declare a hastable type.


a = {}
For example:

a = {1: 'hello', 'lang': 'Python'}
In PHP we declare:

$a = array(1 => 'hello', 'lang' => 'Python'); // or $a = [1 => 'hello', 'lang' => 'Python']

3. Set

We use PHP array key to represent a set item because the key is unique. For example:

$_set = array();
$_set['java'] = 1;
$_set['python'] = 2;
$_set['php'] = 3;

In Python we use curly braces to initialize set. For example:

my_set = {'java', 'python', 'php'} #since 2.7
or

my_set = set(['java', 'python', 'php'])