1/13/16

Building my first directive with angular2

Recently i started to play with recently released beta of angular2.
I'm totally beginner and i want to build very simple thing.
I want to build the component (component is like directive in angular 1.) that displays current time in format given as a parameter (through the attribute format).
  
  
    <now format="'h:mm:ss'"></now>
  
When rendered HTML, should look like this:

How to run angular2?

First thing you need then you want to run angular2 is environment.
You can use one of angular2 starter kits like this one Or just open plunker and select "Editor" -> "New" -> "AngularJs" -> "2.0.x TS" and viola!! you are angular2 programmer!


Component

The first basic thing in angular2 is component, so: Lets create a component:


import {Component} from 'angular2/core'

@Component({
  selector: 'now',
  template: `

{{date}}

` }) export class Now { private date; constructor() { this.date = new Date(); } }

Yoo hoo!!! The component created and running!!!
And it displays the current date!!!
But one little disturbing thing is that date appears raw , which is little bit ugly...

So lets improve the component by formatting the output.


Pipe (Filter)

Like in sngular 1. there are filters in angular2, they called pipes


import {Component} from 'angular2/core'

@Component({
  selector: 'now',
  template: `

{{date | date :'dd/MM/yyyy'}}

` })

Getting value from attribute (The tricky part)

As we already said the desired format should be passed through the attribute. But how to access this attribute from inside component constructor? Fortunately there is Attribute module which enables you to inject attribute into constructor parameters:

import {Component, Attribute} from 'angular2/core'
export class Now {
   private date;
   
  constructor(@Attribute("format") format) { 
    this.format = format;
    this.date =  new Date(); 

  } 

} 

Better way

After Lenin's older brother was executed because he tried(but failed) to kill king of Russia with bomb, young Lenin said "We will go different way"...
After looking at angular2 docs i found better (more angular2mish) way which says that if we want to pass attribute to component we must use square brackets :

  
  
    <now [format]="'h:mm:ss'"></now>
  

I will use @Input module instead of @attribute




import {Component,  Input} from 'angular2/core'

@Component({
  selector: 'now',
  template: `
      

{{date | date: _format}}

` }) export class Now { private date; @Input() set format(formatName:string){ this._format = formatName; } constructor() { this.date = new Date(); } }

How to update time each second?

Little nice thing to add here is ability to update the time displayed in directive each second. For achieve this i'm using ES6 fat arrow syntax


export class Now {
   private date;
   @Input() set format(formatName:string){
     this._format = formatName;
   
   }   
  constructor() { 
    this.date =  new Date(); 
    
    setInterval(() => {
        this.date =  new Date();
     }, 1000);
  } 

} 


View it on plunker:

9/29/15

is it possible to track your visitors individual behavior?

The short answer is "Yes, sure", but there are lot of options how to do it. I will talk now about how to track behavior using google analytics.
I guess  everyone knows how to implement google analytics at your website  - to create account at google analytics and to paste the code snippet like:


    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'your-account-num']);
    _gaq.push(['_trackPageview']);

    (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    })();


This great tool of google makes you able to analyze your website traffic (like getting reports) 

 








without writing any code!
 
The problem with tracking individual user's behavior is that you need analytics service to know the id/name of specific user. Thats where angularticks library can be usefull.
This lybrary enables you easily fire events with data you choose to send:

 var eventData = {  
  category: 'motorcycle-details',
  label: user+' viewed '+$routeParams.productname +' motorcycle'
 };
 $analytics.eventTrack('viewing-motorcycle-details', eventData);

If user name passed through the event parameres the reports will show results with his behavior:



 

9/19/15

how to make google to index your angular single page app

The problem with angular single page app (which uses router module) is that google crawler cannot access route view dynamic content.
When processing such pages (like mysite.com/#/product/zzr1400) google crawler sees only template with "{{something should be here}}" tags.

I have created this site for experimenting purposes.
According to little research I did (see this grate tutorial and here) for make your site to be indexed by google you need to serve crawler bot static HTML snapshots made from your dynamic views.

Steps

For doing it you need to perform following steps:
1. place following tag in the head of your index page:

<meta name="fragment" content="!">

2. make sure your router configured to use "!" or html5 mode

  $locationProvider.html5Mode(true);
  $locationProvider.hashPrefix('!');

This tells crawler to access the site using "?_escaped_fragment_=" quesry string.
3. configure your server or use some online services like Prerender.io or brombone to serve static html snapshots of your content.

How to check you pages are cached for crawler?

If you using prerender.io online service:
Access your dashboard:

You should see the table of your pages in the "cached pages" section.

Getting started with docker

It is very simple to get started usig docker. All you need to do-is download the docker desktop for your system Once you get docker syste...