Alejandro G. Carlstein Ramos Mejia Blog



var Book = (function(){
    // Private
    var _name;
    var _price;
    var priceChanging = [];
    var priceChanged = [];

    var updatePriceChanging = function(_this, value){
        for (var i = 0; i < priceChanging.length; ++i){
            if (!priceChanging[i](this, value)){
                return true;
            }
        }
        return false;
    }

    var updatePriceChanged = function(_this){
        for (var i = 0; i < priceChanged.length; ++i){
            priceChanged[i](_this);
        }
    }

    //Public
    return {
        factory(name, price){
            _name = name;
            _price = price;
            return this;
        }
        ,name: function(){
            return _name;
        }
        , price: function(value){
            if (value !== undefined
                && value != _price){
                var isPriceTooHigh = updatePriceChanging(this, value)
                if (isPriceTooHigh){
                    return _price;
                }
                _price = value;    
                updatePriceChanged(this);
            }                
            return _price;
        }
        ,onPriceChanging: function(callback){
            priceChanging.push(callback);
        }
        , onPriceChanged: function(callback){
            priceChanged.push(callback);
        }

    }

})();

var book = Book.factory('Lord Of the Rings', 23.99);

console.log(book.name());
console.log(book.price());

book.onPriceChanging(function(book, price){
    console.log('Changing book price to $' + price);
    if (price > 100){
        console.log('-> [X] Price ($' + price + ") is too high. No changing price");
        return false;
    }
    return true;
});

book.onPriceChanged(function(book){
    console.log("The book '" + book.name() + ' changed price to $' + book.price());
});

book.price(19.99);
book.price(200.00);
book.price(99.01);

Output:

Lord Of the Rings
23.99
Changing book price to $19.99
The book 'Lord Of the Rings changed price to $19.99
Changing book price to $200
[X] Price ($200) is too high. No changing price
Changing book price to $99.01
The book 'Lord Of the Rings changed price to $99.01

 

Share

If you wish to create a read-only domain object that insure that no body is allowed to write to the database through your domain object, then add these even listener at the end of your domain object file:

transient beforeInsert = {
throw new RuntimeException('insert not allowed')
}

transient beforeUpdate = {
throw new RuntimeException('update not allowed')
}

transient beforeDelete = {
throw new RuntimeException('delete not allowed')
}

 

Share

In this example you will see: Regular quicksort, quicksort using random partition, quicksort using Hoarse partition, quicksort using random partition together with Hoarse partition

NOTIFICATION: These examples are provided for educational purposes. Using this code is under your own responsibility and risk. The code is given ‘as is’. I do not take responsibilities of how they are used.

Share

[Disclaimer: This code is provided for educational purposes, you are responsible of how you use this code and this code is provided ‘as is’. Meaning that I do not take responsibility of  any harm that this code may or not produce]

The following is an example of a client and server that let you transfer a file from the client to the server by encrypting and then decrypting the packages using Vigenere Cipher.

Share

Example of Linked List

This code was compiled and tested on a Linux machine

Share

< Previous (Operative Systems – Part 5) | (Operative Systems – Part 7) Next >

NOTIFICATION: These notes are published for educational purposes. Using these notes is under your own responsibility and risk. These notes are given ‘as is’. I do not take responsibilities for how you use them.

Share

On the previous part of this tutorial, “AngularJS: Tutorial: Part 2: Charts”, I showed you how you can make some nice charts using Angular-Chart.js, a library built by Jerome Toufee-Blin. For example, a reactive chart:

This code works; however, there are some silly mistakes that could bring some troubles in the future. Therefore, here is a list of common mistakes and some suggestions on how to go around them.

Share