Tuesday 21 January 2014

Javascript Quick Recap

Values, variables and literals

    • Variables
      • var a ;  << Default value  of a is undefined and behave as false
        • undefined + numeric = NaN
          var a; a+2  => NaN
        • var a; if(!a) alert("hi"); => alert("hi");
      • use of undeclared variable  result in  ReferenceError exception.
      • Global variables
        • Global variables are infact properties of global objects. eg window in web pages and can be accessed as window.variable
    • Constants
      • const prefix = '212' ;  // Read only variables
    • Concatenation 
      • using  + operator
        "32" + " is a number" =>  "32 is a number"
      • toString()
        • var a = 32;   a.toString() => "32"
    • Type conversion
      • unary operator + will convert the right hand side operand to int
        • +document.getElementById("div0").value
        • 23 == +"23"  => true
      • parseInt() , parseFloat()
    • literals
      • Array
        • var arr = [2, 3 , , "dfd", ]
          • length: 4 // last trailing comma is ignore.
          • empty place will be treated as undefined
        • Important link:
          https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Predefined_Core_Objects
      • Object

        var foo = {a: "alpha", 2: "two"};
        console.log(foo.a);    // alpha
        console.log(foo[2]);   // two
        //console.log(foo.2);  // Error: missing ) after argument list
        //console.log(foo[a]); // Error: a is not defined
        console.log(foo["a"]); // alpha
        console.log(foo["2"]); // two
      • Enumerating all properties of an object
        • for..in loops
          • for(var key in obj)  console.log( obj[key] );
        • Object.keys(o) => returns an array of keys
          • var arrKeys = Object.keys(obj);  console.log(  obj[arrKeys[0] );
        • Object.getOwnPropertyNames(o) => returns array of values
      • Iterator and Generator
        • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators
      • Defining getters and setters
        • defined using object initializers, or
        • added later to any object at any time using a getter or setter adding method
        • get : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/get?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FOperators%2Fget
        • For deleting get/set use delete

Class ,oops 

    • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Details_of_the_Object_Model
    • Inheritance
      • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Inheritance_Revisited

Using call to invoke an anonymous function

In this purely constructed example, we create anonymous function and use call to invoke it on every object in an array. The main purpose of the anonymous function here is to add a print function to every object, which is able to print the right index of the object in the array. Passing the object as this value was not strictly necessary, but is done for explanatory purpose.
var animals = [
  {species: 'Lion', name: 'King'},
  {species: 'Whale', name: 'Fail'}
];

for (var i = 0; i < animals.length; i++) {
  (function (i) { 
    this.print = function () { 
      console.log('#' + i  + ' ' + this.species + ': ' + this.name); 
    } 
    this.print();
  }).call(animals[i], i);
}

References:

  • Core Objects :  https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Predefined_Core_Objects
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide

Monday 9 December 2013

HTML5 FileReader api

HTML5  FileReader api

FileReader

Event fired:               . 
  onload << fired after  reading all file content into the memory.
  onprogress << file reading progress
  onabort << If file reading is aborted
  onerror << problem reading file. Errors could be : File not found, readable error, and file reading aborted

Tuesday 22 October 2013

SDLC: Best of every model

SDLC

Agile, extreme, scrum , waterfall, RAD, kanban etc etc. I won't put any benchmark and won't try to map in good or bad category. Infact, I'd pull the gist from each and would put it on the screen So, that next time when i stuck anywhere I can see the solution .

Chaos theory: It's made by people who were more concern about testing and fixing bugs. So, they made this model. They gist of this model is :  Fix the most important bug right away.

Extreme Programming: YAGNI: You not gonna need it. Implement features which are going to be used by end users. Don't waste time implemention something that's going to be used in future.

Agile: Batch by batch. Keep the batch small and keep processing them till all batch process or final product is ready.

Scrum: Keep the process agile with the help of scrum master and regular meetings.

Kanban: Keep the size of bath in Agile controlled.



Other philosphical notes notes i like are:
         DRY: Do not repeat yourself
         KISS: Keep it simple, stupid .

Saturday 5 October 2013

Multiple File Upload :Laravel4










Issues and their resolutions with multiple file load

  1. var_dump( Input::file("file_field") ) == NULL, for multiple file uploading but works fine for one file.
    1. Check if you have max_upload_file and max_post_size set very low in php.ini file
  2. Input::file("file_field") is giving only one file object.
    1. Verify that you have  file filed set with name attribute containing brackets . eg. Form::file("file_name[]") .
  3. If you're doing some image processing you then might need to extend memory_limit.
    ini_set('memory_limit', '64M'); >> Set it to ini_set('memory_limit', -1)     
log.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Allowed memory size of 134217728 bytes exhausted (tried to allocate 1xxxxxx bytes)' in ...\vendor\filp\whoops\src\Whoops\Handler\PrettyPageHandler.php:123
Stack trace:
Error  Say's that you need to increase memory for your application. Where? In php.ini file.
If you don't have access? then increase dynamically using ini_set()

     4.  Consult your laravel log file if you face any other error . Log file you may find inside storage/log folder.
eg. path/to/your/application/app/storage/logs





Sunday 30 June 2013

Starting php with Laravel4 : Getting Started

It is been a long time since i last used php. If i remember correctly, I used php around 2 year and 6 months back. Since them am fiddling with RoR and java with struts, hibernate , and scala with play.

It's a reminiscence for me.

Ok, lets start with php5 with laravel.

Setup:
  1. Install apache
    1. create config file
    2. enable rewrite module, if not
 Installing apache and php5

sudo apt-get install apache2 php5

Once, apache2 is installed, move to next step of creating a site.
Here is the template of site look like:

<VirtualHost *:80>
    # Host that will serve this project.
    ServerName app.dev
    # The location of our projects public directory.
    DocumentRoot /path/to/our/public
    # Useful logs for debug.
    CustomLog /path/to/access.log common
    ErrorLog /path/to/error.log
    # Rewrites for pretty URLs, better not to rely on .htaccess.
    <Directory /path/to/our/public>
        <IfModule mod_rewrite.c>
            Options -MultiViews
            RewriteEngine On
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^ index.php [L]
        </IfModule>
    </Directory>
</VirtualHost>
Edit it to suit laravel project directory. Replace "/path/to/our/public" with the path to public folder inside laravel project dir.
Now, put this site inside /etc/apach2/site-available
Enable site:
a2ensite site_name
 Enable rewrite module
a2enmod rewrite
Reload apache2
sudo service apache2 reload
Restart apache2
sudo service apache2 restart
Now, you may see your site at following destination

http://localhost

 Now,  lets edit /app/routes.php file and add our "hello world" route

Route::get('/hello', function() {
     return 'Hello world!';
});



Point your browser to :  http://localhost/hello

 


happy coding.. ;)
==============================

Issue:
   Routes not working?
   Solution:  Please verify you have 'rewrite' module enable.  If it's enabled then verify you have write permission to laravel project dir and it's subfolder.
If it's still not working, then google is your friend. 


Reference:
  1.  laravel site config: https://github.com/daylerees/laravel-website-configs 
  2. Routes not working? http://stackoverflow.com/questions/12045881/php-laravel-routing-issue

Wednesday 26 June 2013

openSSH, generating passwd file

First generate Group:
On local system:
            mkgroup -l >> ..\etc\group

On system with user organized in domains
            mkgroup -d >> ..\etc\group

And then password:
                On local:
            mkpasswd -l -u username >> ..\etc\passwd
NOTE: the username specified above must be an existing windows login account.
               
                On system with many domains:
            mkpasswd -u smm08030 -d mycorp >> >> ..\etc\group




NOTE:
If your computer sees multiple domains, you may have to specify the domain
name that the user "qhander" is part of to mkpasswd after the -d flag
(i.e., run "mkpasswd -u YourUser -d YourDomain >> /etc/passwd").
You also need to run "mkgroup -d >> /etc/group" (and, apparently,
"mkgroup -d YourDomain >> /etc/group")...  Also, you most likely have
*two* users with the name "qhander": one local to your machine (with a
small user id), and one in the domain (with UID 18544) -- you log in as
the domain one, but your /etc/passwd contains the local one (with the
wrong SID and UID).  Once you update /etc/passwd with your domain user
information, change the name of the local user to something else (e.g.,

"qhander_local").

Tuesday 11 June 2013

When to use try{}Catch{}?

A system that swallows and masks exceptions isn’t a healthy system.

I'll remember this line whenever i need to put try/catch block in my line as a thumb rule . :)