Showing posts with label Visual Studio Plugin. Show all posts
Showing posts with label Visual Studio Plugin. Show all posts

Saturday, December 07, 2013

Easy way to write Javascript code using TypeScript

In this article, I am just going to explain about how to write a Javascript code easily using Typescript. If you are planning to write Single Page Application (SPA), Responsive web application or large scale web application, then you supposed to write more Javascript code to achieve it. Moreover, it is difficult to handle more javascript code with lots of confusion around variable type, whether object is element or not. Typescript will solve those problems by writing the code in structured traditional way like C# or JAVA.
      Once you have completed the typescript code, it would be automatically converted to Javascript code when you are compiling the project in Visual Studio 2012/2013 or manually converted to Javascript using Typescript compiler (tsc).
   Typescript is open source typed language developing by Microsoft for Javascript. It can be compatible for all web browsers because typescript finally converted to Javascript code which will be used in application. You have to refer only compiled Javascript code instead of typescript code in your HTML page.

You can easily create Interface, Class, Module, Typed variable and do Inheritance.

Prerequisite:
1. Visual Studio 2012 or Visual Studio 2013
2. Typescript Plugin 

Once you have installed above two, you can start write Typescript (.ts) code. Please follow the below steps.

1. Open Visual Studio 2012/2013
2. Create any web project (MVC, SPA, Web Forms). Here I've created SPA project.
3. Add Typescript(.ts) file to your project.
 

 
 








4. Let's create simple Employee class with some variables and methods. And also, how to call a class method.

class Employee {
    firstName: string;
    lastName: string;
    fullName: string;
    age: number;
    constructor(first: string, last: string) {
        this.firstName = first;
        this.lastName = last;
        this.fullName = this.firstName + " " + this.lastName;
    }

    getFullName() { return this.firstName + " " + this.lastName; }
}


var emp = new Employee("Ayyanar", "Jayabalan");
alert(emp.getFullName());

Intellisense:





If you see above code, you can see the typed variable, typed parameter like how we would do in C# code. And also, it supports intellisense.

5. Compile the project which will create Javascript (JS) file and MAP (.map) file automatically. After compiled, you can't see the JS and MAP file in the same directory in Visual Studio. Just click "Show All Files" in project which will show the JS and MAP file in same directory of Typescript(ts) file.  You can include those files in your project. MAP file which maps TYPESCRIPT and JAVASCRIPT FILE and it will be useful for debugging TYPESCRIPT code by referring map file in JS file.




6. Open the JS file. You can see equivalent Javascript code in that file.
 
var Employee = (function () {
    function Employee(first, last) {
        this.firstName = first;
        this.lastName = last;
        this.fullName = this.firstName + " " + this.lastName;
    }
    Employee.prototype.getFullName = function () {
        return this.firstName + " " + this.lastName;
    };
    return Employee;
})();

var emp = new Employee("Ayyanar", "Jayabalan");
alert(emp.getFullName());
//# sourceMappingURL=MyApp.js.map

At the bottom, you are referring the map file to refer TYPESCRIPT file while debugging.

 7. You can refer the above JS file in your ASPX/MVC View/HTML Page.

8. Let see how to create a module and have a class inside the module. And also, how to refer the module class in outside.

module EmployeeModule {
    export class Employee {
        firstName: string;
        lastName: string;
        fullName: string;
        age: number;
        constructor(first: string, last: string) {
            this.firstName = first;
            this.lastName = last;
            this.fullName = this.firstName + " " + this.lastName;
        }

        getFullName() { return this.firstName + " " + this.lastName; }
    }
}


var emp = new EmployeeModule.Employee("Ayyanar", "Jayabalan");
alert(emp.getFullName());


9. Let see how to inherit the class.

module EmployeeModule {
    export class Employee {
        firstName: string;
        lastName: string;
        fullName: string;
        age: number;
        constructor(first: string, last: string) {
            this.firstName = first;
            this.lastName = last;
            this.fullName = this.firstName + " " + this.lastName;
        }

        getFullName() { return this.firstName + " " + this.lastName; }
    }

    export class Manager extends Employee {
        subOrdinates: string[];
    }
}


10. You can have question of how to use jquery inside my typescript code. You can't refer jQuery javascript file inside typescript file. Instead you have to create a typescript file for jQuery JS file by following way like how we would add nuget package.









Once you have created typescript file for jQuery, you can refer it in your own TYPESCRIPT file.

Definitely TYPESCRIPT will going to rock in future for Web/Mobile Application Development.


 You can get more information about typescript in below URL.

http://www.typescriptlang.org/



Saturday, March 02, 2013

Copy your C#/VB Code as HTML or Formatting your C#/VB code into HTML for posting article in Blog's

Usually, when I am writing article with code, I have faced lots of problems on formatting the code into HTML. I used to do the formatting using online codeformatter tools. But its not giving good look and feel. Yesterday,I thought about this while i'm in restroom on why don't we create a plugin in Visual Studio to format the code into HTML. Today, I have just google'd whether anybody created this kind of plugin before for formatting the code into HTML format for blogging. Finally, I found the nice plugin "Copy Source As HTML" .

Here is the list of steps to be followed to experiment this plugin.

1. Download the above plugin and install it for VS 2008/2010/2012.

2. Once you have installed, open the Visual Studio and open any project.

3. Select the code which you want copy as HTML, right click and select "Copy As HTML"

Screenshots:

 
 


 



4. Go the blogger/any other blogging site, paste that code in HTML.

Screenshot:



Formatted Code:


        public ActionResult Index()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
 
            return View();
        }