Wednesday, 16 May 2018

001 KnockoutJS Introduction

KnockoutJS Introduction

KnockoutJs is a JavaScript library written on MVVM  pattern. KnockoutJs is widely used in single page application and it is very easy to learn and implement also. KnockoutJs Provides an easy way to handle Complex data driven interface. KnockoutJs was developed by Steve Sanderson Microsoft employee in the year 2010, but the stable version comes in 2017. KnockoutJs  support almost all modern browsers like Chrome , Firefox ,Opera ,Safari,IE . KnockoutJs is a free  and open source software.

KnockoutJs follow MVVM pattern


Model : Model is responsible for holding data of user interface element.


View : View represent the data from model user interface. View provides structure layout of the user interface and present to the user

Viewmodel : Viewmodel is a mediator between view and model.Viewmodel communicate between view and model to share data and functions. Data menu created by the function that attributed to the user interface. When a data is changed, it is  also reflect in the user interface  as new model is the communicator for any change of data.

This architecture have several benefits, a clear separation between domain data, view component and data to be displayed, present a  clearly defined clear when is the relationship between view and component.

Here are some point about KnockoutJs

  • KnockoutJs is free and open source software.
  • KnockoutJs library is very light and small in size (58kb).
  • Provides a very easy clean  way to handle Complex data driven interface.
  • A lot of documentation is available.

MVVM Model























 
It is very easy to use KnockoutJs. You will find the full 
documentation of KnockoutJs and its usage here.


http://knockoutjs.com/documentation/introduction.html


There are two options, that you can  choose  to use KnockoutJs. 
You can download KnockoutJs library or you can refer the CDN 
of KnockoutJs. To apply the KnockoutJs in your project simple 
add a tag "script" and source(src) of the script tag will be 
either your local file or CDN.
 

There are two type of version available , one is minified and 
another is original version. You can use both.

Below is the example of how you will use KnockoutJs 
downloaded version.
 

Below is the example of how you will used KnockoutJs  from CDN.

 
 
Alternatively you can refer to a minified version of KnockoutJS 

library from CDNJS.
 
 

004 Node.js REPL Terminal

REPL Terminal
Node.js have virtual environment called REPL or Node Shell ,REPL stands for Read Eval Print Loop.REPL is commonly used for debug JS code.
  • Read − Reads user's input, parses the input into JavaScript data-structure, and save in memory.
  • Eval − Evaluates the data structure.
  • Print − Prints the result.
  • Loop − Loops the above command until the user presses ctrl-c twice.

To open REPL , Open command prompt , then type "node".Below is the image




You can evaluate expression in REPL also.



Example 1 (Simple Expression)
10+20
=30

Output





















Example 2 (Complex Expression)
12+((11+3)*9)/3
=54


Output













Variable in REPL 
 
Variables are used to store values. var keyword is used for variable declaration.You can evaluate expression with variable also.



Example 1 (Simple Expression)
> var a=10
undefined
> var b=20
undefined
> a+b
30
Output :

















Multiline Expression  

Node.js REPL supports multiline expression similar to JavaScript.

Example 1 

for(var l=0;l<20 br="" l=""> <20 20="" l="">

<20 br="" l="">{
  console.log(l*l);
}

Output :

 

 


Node.js Underscore Variable 

You can also use underscore _ to get the last evaluated value.

Example 1 

> 1+9+8+6
24
> _
24

Output :

 

 











REPL Commands

help:The .help command is used to display all the  REPL commands.

Example

C:\Users\dbhattacharjee>node
> .help


Output :










exit :Exit the repl

Example 
C:\Users\dbhattacharjee>node
> .exit 

Output :
C:\Users\dbhattacharjee>



save :Save all evaluated commands in this REPL session to a file.

Example
C:\Users\dbhattacharjee>node
> 5+9+3
17
> .save myData.txt
Session saved to:myData.txt




load :Load JS from a file into the REPL session


Example 



C:\Users\dbhattacharjee>node
> 4+5+6+4
19
> .save myFile.txt
Session saved to:myFile.txt
> .load myFile.txt
4+5+6+4

19
>


editor : Enter editor mode
 
Example
C:\Users\dbhattacharjee>node
> .editor
// Entering editor mode (^D to finish, ^C to cancel)
console.log('hello world');
hello world
undefined
>

tab keys :list of current commands.
Example



 REPL Keyboard Commands

(a) ctrl + c :It is used to terminate the current command.
(b) ctrl + c twice :It terminates the node repl.
(c) ctrl + d :It terminates the node repl.
(d) up/down keys :It is used to see command history and modify previous commands.









003 Node.js Get Started

Node.js Get Started
An application of Node.js are mainly three part 
1)Module 2)Create server 3)Read request and return response.


Module : Module is a collection of function that are required for  application .Node.js has a set of built-in modules which be directly used in the application.The syntax of include a module is require(). Below is example of Module.

var http = require('http');

You can create your own module in Node.js application.

Create Server : Server listen to client's request.The syntax create a server is http.createServer().
This method create a instance of a server and it will bind with a port.You can define the port number during server instance creation.Below is example of server instance creation.

http.createServer(function (request, response) {
  
}).listen(8081);


Read request / return response : The server will read the HTTP request from client and response to either console or browser.

Below is the simple example of an Node.js application.

Example
var http = require("http");

http.createServer(function (request, response)  
{
   console.log('Hello World\n');
}).listen(8081);

To test this application
1)Created a file named "main" and extension ".js" , that is "main.js".
2)Open the file and paste the above code and save the file.
3)Open Node.js command prompt.
4)Move to the directory where "main.js" file is saved.
5)Write command node main.js and press enter
6)Below is the output of Hellow world in the browser.

 
 
If you wand to see the output in browser ,you have to write code that
write on response.We are adding this two lines. 

  response.writeHead(200, {'Content-Type': 'text/plain'});
  response.end('Hello World\n');
 
Now , type  http://127.0.0.1:8081/ in your browser url.Here is the result






002 Node.js Environment Setup


Environment Setup for Windows

Node.js is a free open source software .You can download it from https://nodejs.org/en/download/



You will get below screen



1)Download the Windows installer from the Nodes.js® web site.

http://nodejs.org/dist/v0.10.26/node-v0.10.26-x86.msi
http://nodejs.org/dist/v0.10.26/x64/node-v0.10.26-x64.msi

2)Double Click the msi for installation ,you will get below screen

3)Click on run


 
 4)Click Next ,you will get below screen





 5)Accept the License Agreement and click next.






Test your application
Your Setup is ready for run.Now you need to test your application.
Below is the step , you can follow

1)Create a text file and rename is "main" , now change the extension of the file to 'js'.

2)Open main.js file and write the following line

console.log('My first Nodejs Application'); 

Save the text and close the file.

3)Now open "Command Prompt" of your computer.

4)use "cd" command to move to the location , where you have created the "main.js" file.

5)We have created it in  the Desktop.So our path is
C:\Desktop>

6)Type the following command

C:\Desktop>node main.js

You will the output
"My first Nodejs Application".



How to Uninstall Node.js 
1)Open the Windows Control Panel.
2)Choose the “Programs and Features” option.
3)Click the “Uninstall a program” option.
4)Select Node.js, and click the Uninstall link.



Node.js installer are available almost all modern operating system like Android,Linux,Ubuntu,Fedora, FreeBSD , OpenBSD,openSUSE,macOS , SmartOS.Below are some other popular installer

  • Linux  :node-v6.3.1-linux-x86.tar.gz
  • Mac    :node-v6.3.1-darwin-x86.tar.gz
  • SunOS  :node-v6.3.1-sunos-x86.tar.gz
There are separate installation process for separate operating system.Some popular installation procedure are

  • Android :                                                     pkg install nodejs
    pkg install nodejs-current
  • Fedora  :                                                      sudo dnf install nodejs                                                     
  • Enterprise Linux :                                           sudo yum install nodejs npm --enablerepo=epel






















বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...