Posted in Javascript

Angular2 Component Interactions with @Input() @Output() and events

Components are the core of Angular2. It is important and interesting to understand how components interact. I will try to explain the components’ interaction here.

We have 2 components here.

  1. Parent component, let’s call it Mom
  2. Child component, name it Daughter

The child component is contained inside the parent component. It looks like the one below

Let’s consider the cases:
Case 1: The child to parent communication. On click on button “Send message to Mom” button, the message “Hi Mom! Greetings from daughter” is passed from the child to the parent component. It looks like

To achieve this, we do the following at the child component

  1. Create an EventEmitter object inside the child component with  @Output() decorator
        @Output() callMomEvent = new EventEmitter();
  2. Fire the event. It is done inside the callMom() function in the child component.

So, putting it together, the child component looks like as follows


import { Component, EventEmitter,Output } from '@angular/core';

@Component({
    selector: "daughter-component",
    template:
            `<div style="border:blue 2px solid; width:300px;margin-left: 51px;margin-bottom: 24px;">
               <h3> I am Child comoponent, the Daughter</h3>
               <div *ngIf="messageFromMom">Message from Mom: {{messageFromMom}}</div>
                <div><button (click)="callMom()">Send message to Mom</button></div>
             </div>`
     })
export class ChildComponent {
    // Create EventEmitter object
    @Output() callMomEvent = new EventEmitter();
    // emit the event 
    callMom() {
        this.callMomEvent.emit("Hi Mom! Greetings from daughter");
    }
}

And at the parent component, we do the following

  1. Listen the custom event callMomEvent .
     <daughter-component (callMomEvent)="handleCallMom($event)"></daughter-component>

    The callMomEvent is the event fired by the child component and handleCallMom is the function that handles the event. It is defined in the parent component

  2. Receive the data emitted and display it

Putting it together, the parent component looks like as follows

import { Component } from '@angular/core';

@Component({
  selector: 'parent-component',
  template: 
           `<div style="border:red 2px solid; width:800px;">
             <h3>I am parent component, the Mom</h3>
             <daughter-component (callMomEvent)="handleCallMom($event)"></daughter-component>
             <div *ngIf="messageFromChild">
                <h3>Message from Child: </h3>
                {{messageFromChild}}
             </div>
          </div>`,
  styleUrls: ['./app.component.css']
})

export class ParentComponent {
  private messageFromChild;
  handleCallMom(message) {
    this.messageFromChild = message;
  }
}

That’s it

Case 2: Passing data from the parent component to child component

That’s simple. It’s done with input binding. Steps are as follows

  1. At the child component, declare variable messageFromMom using the @Input() decorator
    @Input() messageFromMom : string;
  2. At the parent component, bind messageFromMom as
    <daughter-component [messageFromMom]="greetings"  (callMomEvent)="handleCallMom($event)"></daughter-component></div>
    

That’s it

Posted in Uncategorized

Install and Switch node.js versions in Ubuntu


Node.js has a short release cycle which is good but sometimes it breaks things and deprecates a lot of stuffs.So, as a developer we would like maintain different versions of node.js. nvm is a handy tool to do that. We can install any version of node.js and also switch to a version accordingly. Let’s try nvm and switch versions
To install nvm

 wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash

To load nvm

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
 source ~/.bashrc

– Now to install a node.js version  say 6.3.1.

   nvm install v6.3.1

– To switch  to a version

nvm use v6.3.1

– To see the list of installed versions

nvm list

The currently used version is v6.3.1 marked by ->

->       v6.3.1
         v6.9.5
         v7.4.0
         system
node -> stable (-> v7.4.0) (default)
stable -> 7.4 (-> v7.4.0) (default)
unstable -> 6.9 (-> v6.9.5) (default)
iojs -> iojs- (-> N/A) (default)

– To remove a version

nvm uninstall v6.3.1
Posted in Uncategorized

Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header

Today’s error is the following:
localhost:8100/#/app/login:1 XMLHttpRequest cannot load http://mysite.local/api/login. Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://localhost:8100&#8217; is therefore not allowed access.

I was working on ionic & everything was working fine. Then I tried to use a login API. Suddenly from nowhere, it threw up the error . I guessed, it’s from the server side. But, again it works fine from the apk . Hmm, strange!
Then I installed a chrome-app Allow-control-allow-origin . And then, they he coded happily thereafter. Happy ending for the day!

Posted in Uncategorized

Android set up device for development (?????? no permissions)

Today I was trying to debugging on Device from ubuntu 14.04 & I was badly stuck.
Being new to Android, I was in the setup & debugging maze.
Now I was trying to debug from my smart phone.

        1. First thing I did was installing adb-tools. This lets you connect mobile devices to your machine for debugging
          sudo apt-get install android-tools-adb
        2. sudo apt-get install android-tools-adb
        3. Next Create a file named /tmp/android.rules with the following contents (hex vendor numbers were taken from the vendor list page)
          SUBSYSTEM=="usb", ATTRS{idVendor}=="0bb4", MODE="0666"
          SUBSYSTEM=="usb", ATTRS{idVendor}=="0e79", MODE="0666"
          SUBSYSTEM=="usb", ATTRS{idVendor}=="0502", MODE="0666"
          SUBSYSTEM=="usb", ATTRS{idVendor}=="0b05", MODE="0666"
          SUBSYSTEM=="usb", ATTRS{idVendor}=="413c", MODE="0666"
          SUBSYSTEM=="usb", ATTRS{idVendor}=="0489", MODE="0666"
          SUBSYSTEM=="usb", ATTRS{idVendor}=="091e", MODE="0666"
          SUBSYSTEM=="usb", ATTRS{idVendor}=="18d1", MODE="0666"
          SUBSYSTEM=="usb", ATTRS{idVendor}=="0bb4", MODE="0666"
          SUBSYSTEM=="usb", ATTRS{idVendor}=="12d1", MODE="0666"
          SUBSYSTEM=="usb", ATTRS{idVendor}=="24e3", MODE="0666"
          SUBSYSTEM=="usb", ATTRS{idVendor}=="2116", MODE="0666"
          SUBSYSTEM=="usb", ATTRS{idVendor}=="0482", MODE="0666"
          SUBSYSTEM=="usb", ATTRS{idVendor}=="17ef", MODE="0666"
          SUBSYSTEM=="usb", ATTRS{idVendor}=="1004", MODE="0666"
          SUBSYSTEM=="usb", ATTRS{idVendor}=="22b8", MODE="0666"
          SUBSYSTEM=="usb", ATTRS{idVendor}=="0409", MODE="0666"
          SUBSYSTEM=="usb", ATTRS{idVendor}=="2080", MODE="0666"
          SUBSYSTEM=="usb", ATTRS{idVendor}=="0955", MODE="0666"
          SUBSYSTEM=="usb", ATTRS{idVendor}=="2257", MODE="0666"
          SUBSYSTEM=="usb", ATTRS{idVendor}=="10a9", MODE="0666"
          SUBSYSTEM=="usb", ATTRS{idVendor}=="1d4d", MODE="0666"
          SUBSYSTEM=="usb", ATTRS{idVendor}=="0471", MODE="0666"
          SUBSYSTEM=="usb", ATTRS{idVendor}=="04da", MODE="0666"
          SUBSYSTEM=="usb", ATTRS{idVendor}=="05c6", MODE="0666"
          SUBSYSTEM=="usb", ATTRS{idVendor}=="1f53", MODE="0666"
          SUBSYSTEM=="usb", ATTRS{idVendor}=="04e8", MODE="0666"
          SUBSYSTEM=="usb", ATTRS{idVendor}=="04dd", MODE="0666"
          SUBSYSTEM=="usb", ATTRS{idVendor}=="0fce", MODE="0666"
          SUBSYSTEM=="usb", ATTRS{idVendor}=="0930", MODE="0666"
          SUBSYSTEM=="usb", ATTRS{idVendor}=="19d2", MODE="0666"
          SUBSYSTEM=="usb", ATTRS{idVendor}=="1bbb", MODE="0666"
          
        4. Run the following commands:
          sudo cp /tmp/android.rules /etc/udev/rules.d/51-android.rules
          sudo chmod 644   /etc/udev/rules.d/51-android.rules
          sudo chown root. /etc/udev/rules.d/51-android.rules
          sudo service udev restart
          sudo killall adb

           

        5. Disconnect the USB cable between the phone and the computer.  Reconnect the phone. Run
          adb devices

          to confirm that now it has permission to access the phone.

 

 

Posted in Uncategorized

Downgrade PHP7 to PHP 5.6

 

Then I had to downgrade to PHP 5.6
There are 2 ways to do that. Remove PHP 7.0 completely & install PHP 5.6

sudo apt-get remove php7.0*
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install php 5.6

Keep both PHP 7.0 & PHP 5.6, switch accordingly

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install php7.0 php5.6 php5.6-mysql php-gettext php5.6-mbstring  libapache2-mod-php5.6 libapache2-mod-php7.0

Now to use PHP7.0 use the following command

sudo a2dismod php5.6 ; sudo a2enmod php7.0 ; sudo service apache2 restart

And to use PHP5.6 use the following

sudo a2dismod php7.0 ; sudo a2enmod php5.6 ; sudo service apache2 restart

That’s it!

Posted in Database Doctrine, Uncategorized

ZF2, Doctrine & Oracle

To do: Setting up Doctrine to use Oracle
So, here we go.

  • I assume you have added Doctrine module, if not refer here
  • Next open your
    config/autoload/local.php

    & add the configuration as follows

    return array(
        'doctrine' => array(
            'connection' => array(
                'orm_default' => array(
                    'driverClass' => 'Doctrine\DBAL\Driver\OCI8\Driver',
                    'params' => array(
                        'host'     => '192.168.4.136',
                        'port'     => '1521',
                        'user'     => 'iamuser',
                        'password' => 'xxxx',
                        'dbname'   => 'mydb',
                        'driver'   => 'oci8',
                        'service'  => true,
                        'servicename'=>'godisgreat'
                    )
                )
            )
        )
       );
    
  • Next, install libiao
    sudo apt-get install libaio1

    Then install the oci8 driver . You can dowload it from Oracle’s site

  • Now, enable it in php.ini. Open it
    sudo vi /etc/php5/apache2/php.ini
    

    & add the extension like

    extension=oci8.so
    
  • Don’t forget to restart apache
Posted in Database Doctrine

ResultSetMapping

Doctrine ORM expects every table to be an Entity. But that is not easy , when dealing with complex query and which requires querying the result..
So, a hack to this problem is Resultsetmapping class, which allows you to write sql query and then map the result to Doctrine… Here is an example:

use Doctrine\ORM\Query\ResultSetMapping;
$query =   "select q1.id as id,q1.display_id as display_id,q1.product_id from
                        	(SELECT *
                        	FROM (
                        	SELECT id, display_id,active_flag,hash,product_id
                        	FROM test_case
                        	where  product_id=$productId and active_flag=0
                        	ORDER BY display_id DESC
                        	) a
                        	GROUP BY hash
                        	) AS q1
                	join
                        	(SELECT *
                        	FROM (SELECT id,display_id,active_flag,hash
                        	FROM test_case where product_id=$productId
                        	ORDER BY display_id DESC) b
                        	GROUP BY hash ) AS q2
                	on
                	q1.id = q2.id";
    	$rsm = new ResultSetMapping();
    	$rsm->addEntityResult('Test\Entity\TestCase', 'TestCase');
    	$rsm->addFieldResult('TestCase', 'id', 'id');
    	$rsm->addFieldResult('TestCase', 'display_id', 'displayId');
    	$rsm->addFieldResult('TestCase', 'product_id', 'productId');
    	$query = $this->getEntityManager()->createNativeQuery($query, $rsm);
        $testCaseDrafts = $query->getResult();