Showing posts with label jquery. Show all posts
Showing posts with label jquery. Show all posts

Enable jQuery Intellisense in Visual Studio 2010

Enable jQuery Intellisense in Visual Studio 2010

How to Do That ?

There are 2 ways to do that.

Method 1 :

When you are working with Local copy of js files,Then you have to set up jquery-1.5.1-vsdoc.js file as below.(This is my js file version.It supports These versions )

Then on Master Page Set Script Tags like below.
<head>
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
</head>

After that on Child Pages you can have Jquery Intellisense on each level of your code as below.



Method 2 :

If you are referring js files from CDN (Content Delivery Network) then add vsdoc.js file into your Scripts folder  like below.


Your Master Page should looks like below.
                                      with  Microsoft CDN 
<head>
<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.7.1.min.js" type="text/javascript">
</script>
</head>
                                       with Google CDN

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript">
</script>
</head>


Note : If you set up vsdoc.js file as above then it works with both CDN.

Conclusion

By using above methods you can use jQuery Intellisense with VS 2010.One Very Important thing you have to remember here is both your .min.js file and .vsdoc.js files Versions Must be the Same.Otherwise some wrong Intellisense information may appear for Jquery functions.
Continue Reading

Disable a Button Using Jquery and CSS

Disable a Button Using Jquery and CSS


How to Disable a Button ? :




Step 1:

Using Jquery attr Attribute :

$('.actionButton').attr('disabled', 'disabled');

Step 2:

Using Jquery addClass CSS Class :

$('.actionButton').addClass('disabled');

Step 3:

Then by Declaring CSS Style Class :

    <style type="text/css">
        .disabled
        {
            background: none repeat scroll 0 0 burlyWood;
            cursor: default !important;
        }
    </style>
Note : Important thing here is you have to give proper cursor style like above when button is in Disable State

How to Enable a Button Again ? :

Step 1 :

Using Jquery removeAttr Attribute :

$('.actionButton').removeAttr('disabled');
Step 2 :

Using Jquery removeClass CSS Class :

$('.actionButton').removeClass('disabled');

Conclusion
You can Disable or Enable a button which was disable earlier by using above methods easily.

Happy Coding.

Continue Reading

Enable jQuery Intellisense in Visual Studio 2010

Enable jQuery Intellisense in Visual Studio 2010

How to Do That ?

There are 2 ways to do that.

Method 1 :

When you are working with Local copy of js files,Then you have to set up jquery-1.5.1-vsdoc.js file as below.(This is my js file version.It supports These versions )

Then on Master Page Set Script Tags like below.
<head>
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
</head>

After that on Child Pages you can have Jquery Intellisense on each level of your code as below.



Method 2 :

If you are referring js files from CDN (Content Delivery Network) then add vsdoc.js file into your Scripts folder  like below.


Your Master Page should looks like below.
                                      with  Microsoft CDN 
<head>
<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.7.1.min.js" type="text/javascript">
</script>
</head>
                                       with Google CDN

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript">
</script>
</head>


Note : If you set up vsdoc.js file as above then it works with both CDN.

Conclusion

By using above methods you can use jQuery Intellisense with VS 2010.One Very Important thing you have to remember here is both your .min.js file and .vsdoc.js files Versions Must be the Same.Otherwise some wrong Intellisense information may appear for Jquery functions.

Happy Coding.
Continue Reading

Improve jQuery Selector Performance

Improve jQuery Selector Performance

What is a jQuery ?
  • jQuery is a lightweight, "Write Less, Do More", JavaScript library.
  • The purpose of jQuery is to make it much easier to use JavaScript on your website.
  • jQuery takes a lot of common tasks that requires many lines of JavaScript code to accomplish, and wraps it into methods that you can call with a single line of code.
  • jQuery also simplifies a lot of the complicated things from JavaScript, like AJAX calls and DOM manipulation.
                              What is a DOM ?
                                      - Document Object Model
                                      - is the Backbone of every webpage
                                      - web browser parses HTML and Javascript and maintains
                                        a Data Model of what should be displayed to the user 

The jQuery Library Contains the Following Features:
  • HTML/DOM manipulation
  • CSS manipulation
  • HTML event methods
  • Effects and animations
  • AJAX
  • Utilities
  • jQuery has plugins for almost any task out there.
What is a jQuery Selector?
  • A Selector identifies an HTML element tag that is used to manipulate with jQuery code. 
  • Selectors allow page elements to be selected

Selector Syntax 

$(Selector Expression)          OR      jQuery(Selector Expression)

What are the Types of jQuery Selectors?

1. Class Selectors
       $(".intro")  - All elements with class="intro"

2. ID Selectors
       $("#lastname")  - The element with id=lastname

3. Element Selectors
       $("p")   - All p elements

How to Caching jQuery Selectors ?

What is Happening When We use Selector ?
  • jQuery Traverses through all Elements of DOM every time

How Can Overcome this?
  • You should Always Cache your Selections to some variable
Its Like this :
      var myList = $('.myList');
      myList.text("cached");
When to Use ?
  • Using the Same Selection More than Once in your DOM Tree. 
  • e:g: Inside a jQuery Function Likes below
BAD Way
$(".myList").click( function() {
   $(".myList").hide(); 
});
BEST Way
var myList  = $(".myList");
myList.click( function() {
       myList.hide();  // No need to re-select .myList since we cached it
});

Performance of  jQuery Selector when Caching




More Tips for Optimizing jQuery Selector Performance :
1. Qualify your Selector strings when searching by class
       - give Selector's Context as 2nd Parameter
       - when the branches of your DOM tree become very deep 

                       What is a Context ?
                            - jQuery has an optional second argument called context
                            - Its Limit the search within a specific node
                            - Great when you have a very Large DOM tree and need to find
                             e.g: All the <p> tags within a specific part of the DOM tree
                            - You can check to see what is the context of Selector by using context
                               Property.
                              e.g:    $('p').context; //out put is => document
BAD Way
$(".your-class")
BEST Way

// get the node for the context
var context = $('#myContainer')[0];
$(".your-class", context )
                 
2. Don't Qualify your selector when searching by Id.
      - searching by Id uses the browsers native getElementById method (which is very fast)
BAD Way
$("div#your-id")
BEST Way
$("#your-id")

Performance of jQuery ID vs Class  Selectors

Conclusion
  • Always Try to use Id of a Selector when its possible
  • Always Do Caching for jQuery Selectors before it use
  • Always Use Context with class Selectors

I hope this helps to You.Comments and feedback greatly appreciated.
Continue Reading

Check Spelling in Visual Studio 2010

Check Spelling in Visual Studio 2010

What is HTML Spell Checker ?
  • It's a Free Product
  • It's an Extension for Visual Studio 2010
What Type of  Text Verification Spell Checker Supports ?

  • HTML and ASP.NET element content and attributes
  • HTML style Comments  <-- HTML -->
  • ASP.NET server side Comments  <%-- ASP.NET --%>
  • JScript, C# and C++ Comments  // C++ style comments
  • CSS and C style Comments   /* C style comments */
  • VB and VBScript style Comments  'This is VB comment

What are the Basic Requirements Needed for Run Spell Checker ?
  • Microsoft Visual Studio 2010 Any Edition Except Express Editions                          
  • Microsoft Word 2003, 2007 or 2010

How to Download Spell Checker?
  • You can Download from Here
  • Like Below
How to Install Spell Checker ?
Step 1. Close Visual Studio 2010
Step 2. Just Double Click what you have downloaded
Step 3.
Step 4.
How to Use Spell Checker ?
  • Run Visual Studio 2010
  • Open Your Web Project
  • Go to the Page Where You want to Do the Spell Checking
  • Then Click, Tools ---> Spell Checker

  • Now It's Like Word Document Spell checking
  • That's It.It is Very Simple and Elegant.
  • Enjoy it.
Do You need to Know More Details and Functions about Spell Checker ?
  • Spell Checker Home Here
  • Web Developments Tools Team Blog Post Here
  • How to Run Spell Checker on All ASP.NET and HTML Files in the Solution Here

Conclusion
  • It's a very simple Tool for Use and Configure
  • If you want, you can configure it for as Multi-Language spell checker
  • You can get more details about advance features of spell checker by using above mentioned Links

I hope this helps to You.Comments and feedback greatly appreciated.
Continue Reading

What is Knockout ?

What is Knockout ?

What is Knockout ?
  • Knockout is a JavaScript Library
  • Which helps the creation of rich, desktop-like web UIs
  • It simplifies user interactions and makes interfaces fully responsive to any data source changes
  • Knockout provides a simple two-way binding mechanism to link a data model to an UI,
  • Thus making synchronization between them a breeze

What are the Key Concepts of Knockout ?
1. Declarative Bindings
  • These allow you to connect parts of your UI to your data model in a simple and convenient way
  • With declarative bindings even if you change the DOM all bound pieces stay connected
  • You can bind data to a DOM by simply including a data-bind attribute to any DOM element
2. Automatic UI Refresh
3. Dependency Tracking
  • Every time when your model data has changed all parts associated with it automatically being updated
  • No need to worry about adding event handlers and listeners
  • All that extra work is performed internally by Knockout and observables,
  • Which notify listeners when underlying values have changed


4. Templating
  • when your application becomes more complex and you need a way to display a rich structure of view model data,thus keeping your code simple
  • Knockout has a native, built-in template engine which you can use right away
  • But if you want, a third-party template engine, like jquery.tmpl or Underscore, also can be used
More Features of Knockout
How to Download Knockout ?

Step 1

Step 2

Step 3

How to work with Knockout ?
1. You have to add a reference to the "knockout-2.2.0.js" File which you downloaded
2. Then You have to add Reference to above file inside your master file
     i.e. Inside \Views\Shared\_Layout.cshtml
<script src="@Url.Content("~/Scripts/knockout-2.2.0.js")" type="text/javascript"></script>


That's it.Your done.

Lets try with some basic capabilities of Knockout

   - ASP.Net MVC 3 and Visual Studio 2010 has been used 

CASE 1 : How to use bindings in the View (Static Data) ?
  • For that you can use data-bind attributes
View's Code

<!-- This is a View - HTML markup that defines the appearance of your UI -->
<p>First name: <strong data-bind="text: firstName"></strong></p>
<p>Last name: <strong data-bind="text: lastName"></strong></p>
Javascript Code

<script type="text/javascript">
    //This is a simple Viewmodel
    //JavaScript that defines the data and behavior of your UI
    functionAppViewModel() {
        this.firstName = "Sampath";
        this.lastName = "Lokuge";
    }
    //Activates knockout.js
    ko.applyBindings(newAppViewModel());
</script>
Output

CASE 2 : How to Make the data Editable (Dynamic Data) ?
  • That's why Knockout has a concept of observables
  • These are properties that automatically will issue notifications whenever their value changes
i.e.
  • Not only that the underlying ViewModel data is being updated when you edit ,
  • But that all associated UI is updating in Sync with it too
View's Code

<!-- This is a View - HTML markup that defines the appearance of your UI -->
<p>First name: <strong data-bind="text: firstName"></strong></p>
<p>Last name: <strong data-bind="text: lastName"></strong></p>

<p>First name:<input data-bind="value: firstName" /></p>
<p>Last name:<input data-bind="value: lastName" /></p>

Javascript Code

<script type="text/javascript">
    //This is a simple Viewmodel
    //JavaScript that defines the data and behavior of your UI
    functionAppViewModel() {
        this.firstName = ko.observable();
        this.lastName = ko.observable();
    }
    //Activates knockout.js
    ko.applyBindings(newAppViewModel());
</script>


Output

CASE 3 : How to Define Computed Values ?
  • To handle this, Knockout has a concept of Computed Properties
  • These are observable (i.e., they notify on change)
  • And they are computed based on the values of other observables
View's Code

<!-- This is a View - HTML markup that defines the appearance of your UI -->
<p>First name: <strong data-bind="text: firstName"></strong></p>
<p>Last name: <strong data-bind="text: lastName"></strong></p>
<p>First name:<input data-bind="value: firstName" /></p>
<p>Last name:<input data-bind="value: lastName" /></p>

<p>Full name: <strong data-bind="text: fullName"></strong></p>


 Javascript Code

Note :  In order to simplify things you can create a variable self (i.e. var self = this; )
<script type="text/javascript">
    //This is a simple Viewmodel
    //JavaScript that defines the data and behavior of your UI
    functionAppViewModel() {
        varself = this;
        self.firstName = ko.observable();
        self.lastName = ko.observable();
        self.fullName = ko.computed(function () {
            returnself.firstName() + " " + self.lastName();
        });
    }
    //Activates knockout.js
    ko.applyBindings(newAppViewModel());
</script>
Output


CASE 4 : How to add UPPER-CASE behavior ?
  • Need to add a capitalizeLastName function to the viewmodel
  • To Read or Write an observable's value, you have to call it as a function
View's Code

<!-- This is a View - HTML markup that defines the appearance of your UI -->
<p>First name: <strong data-bind="text: firstName"></strong></p>
<p>Last name: <strong data-bind="text: lastName"></strong></p>
<p>First name:<input data-bind="value: firstName" /></p>
<p>Last name:<input data-bind="value: lastName" /></p>

<p>Full name: <strong data-bind="text: fullName"></strong></p>

<button data-bind="click: capitalizeLastName"> Go CAPS </button>

 Javascript Code

<script type="text/javascript">

   //This is a simple Viewmodel
   //JavaScript that defines the data and behavior of your UI
   functionAppViewModel() {
        varself = this;
        self.firstName = ko.observable();
        self.lastName = ko.observable();

        self.fullName = ko.computed(function () {
           returnself.firstName() + " " + self.lastName();
        });

        self.capitalizeLastName = function () {
            varcurrentVal = self.lastName();//Read the current value
            self.lastName(currentVal.toUpperCase());//Write back a modified value
        };
    }
    //Activates knockout.js
    ko.applyBindings(newAppViewModel());
</script>


Output




Small discussion about Knockout on stackoverflow, Which I have involved

What Says Creator of Knockout about This Article ?





Conclusion
  • This is a very basic example, but it illustrates some of the key points of Knockout
  • You've got a clean, object-oriented representation of your UI's data and behaviors (your viewmodel)
  • Separately, you've got a declarative representation of how it should be displayed visibly (your view)
  • You can implement arbitrarily sophisticated behaviors just by updating the viewmodel object
  • You don't have to worry about which DOM elements need to be changed,added or removed ,
  • The framework can take care of synchronizing things for you

I hope this helps to You.Comments and feedback greatly appreciated.
Continue Reading