Blog Spot!


Define a JavaScript Class

Here are the three ways I use to create classes.

Object-like Class, no instantiation is needed.

var name = {
    methodOne: function() {
        // code
    },
    methodTwo: function( param ) {
        // code
    }
};

name.methodTwo('hello');

Function-like Class using prototype, it must be instantiated before usage.

!! pattern NO GOOD !!
private properties are the same for all instances, more like STATIC properties!

// namespace 'Office'
var Office = Office || {};

Office.Users = (function() {

    // constructor
    function user() {
        console.log(isAdmin('pesho'));
    }

    // private properties, more like STATIC properties!
    user.username = 'unknown';
    user.age      = 'unknown';

    // public properties
    user.prototype.state  = 'development';
    user.prototype.deploy = false;

    // private method
    function isAdmin(name) {
        return name == 'admin' ? true : false;
    }

    // public methods
    user.prototype.setUsername = function(username) {
        user.username = username;
    };

    user.prototype.getUsername = function() {
        return user.username;
    };

    user.prototype.setAge = function(age) {
        user.age = age;
    };

    user.prototype.getAge = function() {
        return user.age;
    };

    return user;

}());

var test = new Office.Users();

Function-like Class, it must be instantiated before usage.

function cityLocation() {
    var city = "Paris";

    return {
        get: function() {
            console.log(city);
        }, 
        set: function(newCity) {
            city = newCity;
        }
    };
}

var myLocation = cityLocation();

myLocation.get();           // output: Paris
myLocation.set('Sydney');
myLocation.get();           // output: Sydney

JavaScript is a very flexible object-oriented language when it comes to syntax. It’s important to note that there are no classes in JavaScript. Functions can be used to somewhat simulate classes, but in general JavaScript is a class-less language.

Added on 18.Dec.2014
Tags: js class

Install LAMP with one single command

Ever wondered if you could install the LAMP stack (on Linux Mint/Ubuntu) with one simple command? One would say let's do:

$ sudo apt-get install php5 mysql-server apache2

However, there's a shorter way of accomplishing that. Try:

$ sudo apt-get install lamp-server^

Or if you like things a bit more graphical, you could do:

$ sudo tasksel install lamp-server

And your LAMP is ready!

Source

Added on 17.Dec.2014
Tags: lamp linux apache2 mysql php

Vanilla JavaScript window resize event

jQuery is just wrapping the standard resize DOM event, eg.

window.onresize = function(event) {
    // code..
};

jQuery may do some work to ensure that the resize event gets fired consistently in all browsers, but I'm not sure if any of the browsers differ, but I'd encourage you to test in Firefox, Safari, and IE.

Stack Overflow

Added on 17.Dec.2014
Tags: js resize event

Retrieve an HTML element's actual width and height

window.onload = function()
{
    window.WIDTH  = document.getElementById('id').offsetWidth;
    window.HEIGHT = document.getElementById('id').offsetHeight;
}

! Adding window to the variable makes it global.

You should use the .offsetWidth and .offsetHeight properties. Note they belong to the element!

Beware! offsetHeight/offsetWidth can return 0 if you've done certain DOM modifications to the element recently. You may have to call this code in a setTimeout call after you've modified the element.

Stack Overflow

Added on 17.Dec.2014
Tags: js html width height

Basic PHP socket programming

Learn about the PHP socket programming functions and how to create a daemon/service. There are two videos explaining the code that you can find here: link

Socket Programming: Video 1 Video 2

We all know HTTP is connection-less protocol, but socket programming allows you to stay connected to your users’ browser and update any information you want on the page.

Create the connection:

$socket = socket_create(AF_INET,SOCK_STREAM,0)

Binding to a specific port:

PHP
$ip = "127.0.0.1";
$port = 123;
socket_bind($socket,$ip,$port)

Listening to your customers:

socket_listen($socket)

Accept any requests:

$client = socket_accept($socket)

Server sends message to user:

// welcome the user
$message = "\n Hey! Welcome to another exciting talk! \n\n";
socket_write($client, $message, strlen($message));

Server receives message from user:

$clientMssg = socket_read($client,2048,PHP_NORMAL_READ)

Added on 15.Dec.2014
Tags: php socket

Search


PHP Libraries


Carbon lib / docs
Idiorm lib / docs
Image Workshop lib / docs
lorenzos/Minixed lib / docs
Parsedown lib / docs
PHP Paginator lib / docs
PHP Redis lib / docs
QrCode lib / docs
Requests lib / docs
Slim lib / docs
Spyc lib / docs
TWIG lib / docs
Upload lib / docs
Validation lib / docs
Zebra Image lib / docs

JS Libraries


AJV lib / docs
BackboneJS lib / docs
Bootstrap Notify lib / docs
C3.js lib / docs
ChartJS lib / docs
FastMD5 lib / docs
HighlightJS lib / docs
jQuery-Storage lib / docs
JS-Cookie lib / docs
Leaflet JS lib / docs
LowDB lib / docs
Marked lib / docs
NeedlyJS lib / docs
ParcelJS lib / docs
RequireJS lib / docs
Swig lib / docs
Toastr lib / docs
Underscore lib / docs
ValidateJS lib / docs
top