Saturday, January 2, 2016

ES6 Modules

Exporting and Importing

"export" keyword is used to expose parts of the code to other modules.
You can place "export" keyword in front of any variable, function or class to export it from the module.

Once you have module with exports, you can access the functionality in another module by using the "import" keyword.

The above example import only "sum" from the example module.
If you want to import multiple identifiers from the example module, you can explicitly list them out.

You can also import the entire module as a single object. All of the exports are then available on that object as properties.


Exporting and Importing defaults

The default value for a module is a single variable, function or class as specified by the "default" keyword.

This module exports a function as the default. The "default" keyword indicates that this is a default export and the function does't require a name because the module itself represent the function.

You can import a default value using the following syntax.

For modules that export both a default and one or more non-defaults. For instance suppose you have this module.

You can then import both "name" and the default function using the following.

Resource
https://leanpub.com/understandinges6/read#leanpub-auto-modules

Sunday, November 22, 2015

The Real JavaScript

Hi, In this article i am going to explain the internal architecture of the JavaScript object. After reading this article you have a clear understanding of JavaScript Object, what is __proto__ and prototype chaining in JavaScript.

lets start :)

There are three ways to create an object in JavaScript.

1. Using an object literal

var Person  = {};

2. Using the new keyword

var Person = new Object();

3. Inheriting Object.prototype

var Person = Object.create(Object.prototype);

All of these create same person object.
Here var Person = Object.create(Object.prototype); means that Person object extends from Object.prototype Object.

If you want to add some property to the Person object then this can be added as below.

Person.name = "deepak";

Now Person Object has one property name of type string and has value deepak in it.

So lets understand this simple object by structure.


In the above image you can see the structure of Person Object. Here Person Object extends from Object.prototype Object.

__proto__ provides link between the objects.

For a property it will look into the Object
if unable to find then it will look into the Object.__proto__
if unable to find it will look into the Object.__proto__.__proto__ and so on, until it find null, this is called prototype chaining in JavaScript.

Longer the prototype chain more the access time.

Here Person object accessing hasOwnProperty() method by prototype chaining.

Wednesday, November 4, 2015

What is IIFE in JavaScript

IIFE means Immediately Invoked Function Expression.
It is just an anonymous function that is wrapped inside of a set of parentheses and invoked immediately.
We use IIFE to place all our code inside of a local scope or to avoid global scope.

Structure of the IIFE


Example


In the above example, the code outside of the IIFE has a global scope and the code inside of the IIFE has a local scope. Any variables declared inside of an IIFE can not be assessed from the outside of that IIFE.
It is one of the best practice of JavaScript to avoid global variables and functions. There are lots of design patterns in which we can avoid global variables and functions, IIFE is one of them.

Link of program
IIFE Program

Saturday, August 1, 2015

Basic Git Commands

What is Git
Git is a revision control system. It keeps track of changes.

1. show all branch
This git command list down all your local git branch and also highlight the branch name you are currently in.

2. create new branch
This git command create a new branch named 'newBranchName'.

3. switch to another branch
This git command switch to 'newBranchName' branch.

4. create a new branch and switch to it
5. delete a branch

6. rename the current branch

7. rename the branch which is not your current branch

8. status
This git command list the files you have changed and those you still need to add or commit.

9. add single file
add one file to staging

10. add all files
add all files to staging

11. commit changes to head (but not yet to the remote repository)
12. send changes to the branch of your remote repository
13. update from the remote repository
fetch and merge changes from the remote server(master branch) to your working branch

14. get list of commit id

15. show difference (changes) between two commit id

16. show difference (changes) between two branch

17. changes local working directory code to specific commit

18. show GUI

19. undo git add for single file

20. undo git add for all files

21. undo git commit

22. view your remote branches

Saturday, June 20, 2015

Developing MEAN Application Part 2

In the part 1 of making MEAN application, we have done the server side coding. Now in this second part we will do the front-end coding in AngularJS.

So lets start

We are using AngularJS, so we have to install AngularJS through bower. but first we need to add bower.json file in our project. So add bower.json file by typing following command in terminal.

bower init



This will add bower.json file in our project.


Now we can install AngularJS in our project by below command.

bower install angularjs --save

This will add a bower_component directory in our project which has AngularJS library and bower.json file is updated with AngularJS dependency.



Now we will create a index.html file in our project. So the directory structure of our MEAN application become


Now write following lines of code in index.html file.

Our server is running at 3000 port so when we hit the URL http://localhost:3000/ then index.html page should be rendered on the browser. But this is not happening right now. For rendering index.html page on browser, we have to write the following code in app.js.

Here Above lines of code redirect our server to index.html page and Now when you hit the URL http://localhost:3000/ then this will render index.html in your browser.
We also have to include AngularJS library in index.html page and for that we have to write below code in app.js

Now we can include AngularJS library in index.html page.


Now we code AngularJS simple program which display application name in browser. Thus our index.html page modified as follows.

Now we create a angular controller file userController.js. So the structure of our application become.


And write following lines of code in userController.js

and include userController.js in index.html page

Now hit the URL http://localhost:3000/ in browser and you will see the output as follows


Now we will write AJAX calls for GET Request to get all users, DELETE Request to delete a user, PUT Request to edit a user and POST Request to add a user. We are using $resource service for making AJAX calls. Here is a very good video link of using $resource service https://www.youtube.com/watch?v=X_NZr_-RaLw

Install angular-resource by following command.

bower install angular-resource --save

This will install angular-resource in your project. Now include angular-resource in index.html page.



Now we should create a service for handling AJAX Request because this is not the responsibility of controller. So create a service file userService.js. Now final structure of our MEAN application become.


Write following lines of code in userService.js file

Now include userService.js in index.html page

Now we will write a GET Request to get all users and display all users in table. for getting all users in controller our userController.js file modifies as follows.

Now we have all users in $scope.users variable and we will use $scope.users variable to display all users in table. So the index.html file become.

Now if you hit the URL http://localhost:3000/ then you will see the all users in table.


There is only one user in my database that is why it is showing only one user in table. Now we will add some css so that this table look better. so add following lines of css in index.html page


so the index.html page become


Now when you hit the http://localhost:3000/ URL then you will see the below output in table.


Now we will implement the functionality of adding a user. So first we will write a html code for adding a user. Now our index.html page modifies as below.

And now we will write the controller code. So the controller code modifies as below.


Now when you refresh the http://localhost:3000/ then you will see the below output


When you enter the user name in the input box and click on the add user button then a user is created. for example if you write the user name deepak and click on the add user button then the output would be


Now we will implement the edit functionality of user. So the index.html page modifies as below.

And the userController.js modifies as below.


Now when you refresh the http://localhost:3000/ then you will see the below output


When you click on the edit button of respective user then that user name will be display in the edit user name input box. Now you need to change the name of user and click on the edit user button. If the PUT Request is successful then it will reflect in the user table. Now we will implement the delete user functionality. So the index.html page modify as below.

And the userController.js modify as below.


Now when you refresh the http://localhost:3000/ then you will see the below output


Now if you click on the delete button of respective user then that user is deleted and our user table changes accordingly.
We are done with our MEAN Application. You can also find code of this MEAN application on my Github account. Here is the link

Resources

Saturday, June 6, 2015

Developing MEAN Application Part 1

MEAN stands for MongoDB, Express.js, AngularJS and Node.js. By using MEAN stack we can make end-to-end web application.

MongoDB    -    Database, used to store data

Express.js    -    Node.js web application framework, framework for Node.js so that
                          we do not have to deal with core Node.js

AngularJS    -    Frond-end framework

Node.js        -     Application Server, by using Node.js we can write back-end code in
                          JavaScript

I am considering that you have basic knowledge of each MEAN technologies. Here we are making end-to end web application using MEAN stack. In the part 1 of this tutorial we are doing server side coding that is Node.js, Express.js and MongoDB and test all our CRUD operation in Postman - REST Client. In the part 2 of this tutorial we will do the front end coding in AngularJS.

What we are making
We are making a very simple web application.
As you can see from the image, we can create user, edit user, delete user and get users. And these CRUD operation goes end-to-end. When we talk about end-to-end it means when we create user then it created a user entry in mongoDB database and when we delete user it also delete user from mongoDB database. Similarly on edit it edit user to mongoDB database and on refreshing the page it gets all users from mongoDB database.

So lets start

Open your IDE or editor you are using and create a new project and name it as MEAN_App.
Create a file app.js in your project.


you have to install express.js and mongodb npm package in your project. first create a package.json file. Open terminal and write command

     npm init

press enter 2-3 times and it will create a package.json file in your project.


Now you can install express.js in your project by below command.

npm install express --save

And mongodb npm package by below command.

npm install mongodb --save

This will install express.js and mongodb npm package in your project and you can see that, express and mongodb are added to your package.json file.


Now your project has 2 files and 1 directory


Now we can write code. First we create a connection to mongoDB database and write POST request.

write following lines of code in app.js


The above code does not create a database to mongoDB. It just create a connection to mongoDB database. We have to write a POST request for creating a database. And when POST request execute it create a database named MEAN_Demo and make a user entry to collection.

Now you have to start your mongoDB by tying below command in terminal.

mongod

Your mongoDB is started. Now you can run server by typing below command

node app



If node app command runs successfully then you will see the above output in terminal.

Now we will write a POST request. but before writing a POST request install body-parser npm package by typing below command in terminal.

npm install body-parser --save

This will install a body-parser npm package in your project. And you can see that in package.json file. Now we can write a POST request.

write below lines of code in app.js for POST request.

We have updated app.js file so we have to start the server again to see the changes.
So terminate node app process and run it again by typing node app command in terminal.

Now open Postman - REST Client


Click the image to see it in bigger size. By this POST request database named MEAN_Demo is created in mongoDB database. A user entry in user collection is also created. You can see that in Robomongo.


You just created a database and make a user entry. Similarly we can add another user to database using Postman - REST Client.


Now you can see in Robomongo, there will be two users.


Now we will write a GET request to get all users from database.

Write following lines of code in app.js for GET request.

We have updated app.js file so we have to start the server again to see the changes.
So terminate node app process and run it again by typing node app command in terminal.

Now go to Postman - REST Client


We have created a GET request and get all users from database in Postman - REST Client. Now we will write a PUT request to edit a user.

Write below lines of code in app.js

Above code update a user by _id. So we have to give user id in URL param and new name in request body.
We have updated app.js file so we have to start the server again to see the changes.
So terminate node app process and run it again by typing node app command in terminal.

Now go to Postman - REST Client



In above PUT request we are passing id of user in URL param and new name in body. Here we have updated the name of user deepak to piyush and you can see that in Robomongo.


As you can see from above image we have updated the user name deepak to piyush. Now we will write a DELETE Request to delete a user.

Write below lines of code in app.js

We have updated app.js file so we have to start the server again to see the changes.
So terminate node app process and run it again by typing node app command in terminal.

Now go to Postman - REST Client



We have deleted the user named piyush and we can see it in Robomongo.


As you can see from above image, we have deleted the user named piyush and there is only one user named chetan in mongoDB database.

We have completed all the CRUD operations that is create user, get users, update user and delete user. In the second part of this tutorial we will do the front end coding in AngularJS. Here is the link of second part http://nothingbeyondjavascript.blogspot.in/2015/06/developing-mean-application-part-2.html

Tuesday, May 19, 2015

MVC in AngularJS

Angular JS supports MVC style of application design. MVC means Model-View-Controller. Lets understands each of this in Angular JS.

Controller

Controller responsibility is to wire up the $scope (which holds your models) to your view. Angular Model lives inside a controller, you use $scope inside a controller as a glue between controller and view.
Controller contains business logic behind the application to decorate the scope with functions and values.

Example : controller creation


Model

A model in angular can be a primitive type such as string, number, boolean or a complex type such as object. In short model is just a plain javascript object.
Model lives inside a controller.
Scope is an object and models are the properties of a scope

Example : model creation

Here name is model of type string and name is the property of $scope object.

View

View is what the users see, in angular view is HTML

Example : view creation


Lets combine all components that is Model-View-Controller


model contains your data and with the help of $scope in controller, your model data will be available to your view

Link of Model-View-Controller example

Resource
http://mrbool.com/mvc-in-angularjs/28962