Skip to main content

Posts

Showing posts with the label Javascript

Meteor tutorial - Part II - Building mobile applications

In my previous blog post , I have explained basics of meteor. In this tutorial I am going to add mobile support to the existing meteor application. Installing mobile SDKs Before going to start we need to install IOS and android SDKs (Software development kit). Here I am installing SDKs for iOS. Please keep in mind this is one time process. For installing iOS SDKs, we need to invoke “meteor install-sdk ios”. The same way you can install android SKDs by invoking “meteor install-sdk android” command. Adding mobile platform support I am using already created application called “myapp” and going to add iOS platform support by invoking a command “ meteor add-platform ios ”. We can add android platform by invoking a command  “ meteor add-platform android ”. Running application on mobile emulator Once we add mobile platform then we can run our application by using real mobile device or emulator. I have added IOS platform to myapp already. Now I am going to ru...

Meteor tutorial - Part I

What is Meteor? Meteor is a complete open source platform for building web and mobile application by using JavaScript. It is a combination of NodeJs, MongoDB and client side JavaScript. Installing meteor Meteor supports windows, linux and Mac operating systems. Invoke below command in terminal for installing meteor in mac operating system. Check below link for installing meteor on other operating systems. http://docs.meteor.com/#/basic/quickstart Creating an application The command “meteor create <<app_name>>” is used for creating new meteor application. I have created an app called “myapp” and will see how we can run the application. Running an application “meteor run” command is used for running meteor application. Internally it start a node js server and by default that will listen 3000 port number. Just make sure you are in project directory (myapp) before invoking run command. ' Other useful meteor commands Meteor provides lot of usefu...

GRUNT – Cleaning and Minifying JavaScript Files

In this tutorial, we are going to create a NodeJS/Grunt project which has basically used for minifying many JavaScript files into one. The steps are, Step 1.  Install NodeJS latest version and refer “http://nodejs.org/download/” link for more information. Step 2.  If you are having any existing NodeJS instance then just update by using “npm update –g npm” command. I am having existing NodeJS instance and updated by invoking above command. Step: 3. Next, we need a Grunt command line interface (CLI) tools globally. It is a node.js module and needs to install by using a command called “npm install –g grunt-cli”. Step 4.  We need another component called grunt-init globally. It is also a node.js module and we can install it globally by invoking a command “npm install –g grunt-init”. Step 5.  Next, we need to create a new grunt project. I have created new folder called “Grunt-MinifyDemo” in “D“ directory and this is my...

JavaScript - An Example for Inheritance

Everyone of us know, Inheritance is a way to acquire properties and methods of another object. In other words it is a way to establish is-a relationship between two objects. The static languages by default provide some easier ways to achieve this. For an example, we have "extend" keyword in Java for extending other object. JavaScript is protypal inheritance language (class-free) means that objects can inherit properties from other objects. Let's see an example. Let's create a parent class for Animal /** * Animal - Constructor and parent class for all animals. * @param {Object} aType - Type of the animal * @param {Object} aName - Name of the animal */ function Animal(aType, aName) { this.type = aType; this.name = aName; } Animal.prototype.eat = function() { console.log('All animal will eat'); }; We have to do following steps for extending animal class. 1. Calling super class constructor from child class constructor. 2. Setting up prot...

JavaScript - An example for Singleton pattern

The singleton pattern is a design pattern that restricts instantiation of the class to one object. This pattern is very handy when one object is enough for doing some operation across the entire system. Let's see an example. /** * Single global namespace for an entire application with lazy loading. * @Singleton pattern */ var PUS = PUS || {}; PUS = (function(){ //Private variables and methods var appVersion = 0.1, instance = null; function constructor() { console.log('Object has created'); return { getAppVersion : function() { return appVersion; } }; }; return { getInstance : function() { if(!instance) { instance = constructor(); } return instance; } }; })(); Here PUS object contains one public method called "getInstance". If we call PUS. getInstance() method, that will first check whether an object is already created or not. The object is already exists then will return the reference of an existing object, ot...

Static members in JavaScript

JavaScript static variables and methods are associated with the class, rather than with any object(similar to Java). Every instance of the same class has access to static members and rights to modify the values and method behavior at run-time. Let's see an example. var Person = function(fName, lName) { this.firstName = fName; this.lastName = lName; Person.objectCounts++; }; //Static variable. Person.objectCounts = 0; //Static method. Person.getObjectInformation = function() { return Person.objectCounts; }; Here the variable objectCounts is a static variable and getObjectInformation is a static method. If we create any object for Person class, the static variable "objectCounts" value is incremented by one. The method getObjectInformation is used for getting "objectCounts" value. var person1 = new Person('Suriya', 'A'); var person2 = new Person('John', 'H'); Here there are two objects created for Person class. Let...

Object Oriented JavaScript - Part I

What is JavaScript? JavaScript is a prototype based scripting language with dynamic typing and has first-class functions. Previously it is mostly used in web and mobile browser application development. These days it is used for performing server side operation. The best example is Node JS. What is static and dynamic languages? In static languages, the process of verifying & enforcing constraints of type and identifying the behaviour of the code can happens at compile time itself. Some of the static languages are Java, C#, etc., In dynamic languages the above process is can happens only at run time. This feature will helps to include new code, modifying type systems, and extending objects at run time. What is first-class functions? In many languages like Java and C# have some options for defining class. For an example, this is my Person class in Java. class Person { String firstName; String lastName; } In JavaScript we don't have any keyword like...

Getting key/value pair from JSON object and getting variable name and value from JavaScript object.

 Hi, I had faced one issue like this. I have an JSON object but I don't know any key name but I need to get the all the key and corresponding value from JSON object using client side JavaScript. Suddenly I wondered whether it's possible or not, after that I had done lot of workaround and finally got this solution. See the below example.    function getKeyValueFromJSON() {     var jsonObj =  {a:10,b:20,c:30,d:50} ;     for ( var key in jsonObj) {       alert( "Key: " + key + " value: " + jsonObj[key]);     }  }  In this example I have created the one json array as string, and converted this string into JSON object using eval() function. Using for-each loop I got all the key value from jsonObj, and finally using that key I got the corresponding value.  Finally I got the alert like this,    Key: a value:10    Key: b value:20    Key...

Radio button validation using javascript.

<html> <head>   <script type="text/javascript">     function radioValidation(elementID,elementName)     {      var selected = "";      var radioObj = eval("document.forms[0]."+elementID);      for(var count=0;count<radioObj.length;count++){        if(radioObj[count].checked) {          selected = radioObj[count].value;          break;        }      }      if(selected == ""){         alert("Please selected your "+elementName);         return false;       }      alert("You have selected: "+selected);         }   </script> </hea...