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

Saturday, May 9, 2015

Convert the local time to another time zone in JavaScript

In JavaScript, Date() object get time from your local system. And what, when you deploy your website to server and your server is in the Germany then it get time according to the Germany time zone And you want Indian time whatever your server location.

I recently face this situation. I am working on a website which runs only 8PM to 4AM according to the Indian time zone. So I found a solution for this on the internet which i am writing here.

before that you have to understand the offset, In JavaScript we can get offset by getTimezoneOffset() method of Date object and it gives the difference between UTC time and your local time.

For Example
Your server has Singapore time zone and you want indian time.

var d = new Date();
var time = d.getTime();    // time in milliseconds

var offset = d. getTimezoneOffset();      // gives -480

This is the offset of Singapore and tells that Singapore is ahead of UTC time by 480 minutes
-(minus) specify ahead of UTC time and +(plus) specify behind of UTC time.
Like, if UTC time is 1 PM then Singapore time is 9 PM

var offset = offset * 60 * 1000;

So for getting UTC time

var UTC = time + offset;
This is the actual UTC time which is located in London at Greenwich from where all other country time is calculated.

Now we have UTC time, for getting Indian time we have to know the offset of the India. and Indian offset is +5.5 hours, it means india is 5.5 hours ahaed of UTC time, thus by adding this to UTC time we can get the Indian time

var indianOffset = 5.5 * 60 * 60 * 1000;    // in milliseconds

var IndianTime = UTC + indianOffset;

var nd = new Date(IndianTime);

// this nd is indian time.

Resource
http://www.techrepublic.com/article/convert-the-local-time-to-another-time-zone-with-this-javascript/

Saturday, May 2, 2015

Introduction to mongoDB database

In MongoDB

Collection    :   Table
Document    :   Row
Field            :   Column

1. Find all documents of items collection

2. Find documents of items collection whose price is 10

3. Find documents of items collection whose price is greater than 20

4. Find documents of items collection whose price is greater than 20 and less than 100

5. Loop through all documents of items collection and print JSON

6. Find first two document of items collection

7. Skip first two document of items collection and display others

8. Insert a document in items collection

9. Find first document of items collection
db.items.findOne(); is same as db.items.find().limit(1);

10. Find all document of items collection where name is ‘daal’ or ‘daal makhni’

11. And condition

12. Or condition

13. $and and $or both condition

14. Start mongoDB shell
      This will connect you to running mongod instance

15. Show the list of databases

16. Show current db

17. If database nite-foodie-internal is exist then this command switch to nite-foodie-internal database. If database nite-foodie-internal is not exist then this command create database named nite-foodie-internal

18. Deleted all collection of current database

19. Show all collections of current database

20. Create collection items in current database

21. Delete collection items of current database

22. Update query
      Update the title

23. Delete documents where title equal to ‘new title’ of items collection

24. Delete all documents of items collection

25. Find all documents of items collection and display only name price and type of each document.
In MongoDB when you execute find() method, then it displays all fields of a document. To limit this you need to set list of fields with value 1 or 0. 1 is used to show the field while 0 is used to hide the field.

26. Sort is used to sort data
      1 is used for ascending order while -1 is used for descending order

27. This query will loop through all documents of collection items and sum all price of same type
{ "_id" : "egg", "total_price" : 30 }
{ "_id" : "non-veg", "total_price" : 20 }
{ "_id" : "veg", "total_price" : 161 }

28. Create backup

29. Restore database nite-foodie-internal to mongoDB

30. Returns the count of documents that would match a find() query

31. Apply regex in mongoDB query

32. How to export MongoDB database in CSV format