Thursday, 4 December 2008

jQuery Basics

I’m going to demonstrate a few basic jQuery stuff that you should know before delving into jQuery. If you are new to Javascript you need to be starting here[link]. This might help[link] too. The audience for this tutor are ideally JavaScript novices who want to make a start with jQuery.Let’s start by understanding this small example -


the window.onload() event.




<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Basics</title>
</head>
<body>
<script type="text/javascript">
window.onload = function(){ alert("Hello JavaScript!, document Loaded."); }
</script>
</body>
</html>



Now, As you are already aware, the window.onload event is fired when the HTML window loads. In the above code, look at the Javascript, on window.onload() event, we are calling a function, which alert’s the text “Hello Javascript!, document Loaded.”. This code usually runs smoothly unless for instance, if you are loading a lot of images, Then Javascript code isn’t run until all images have finished loaded, including banner ads etc.


Now, We have an interesting problem, In your window.onload() event, if you want to manipulate your document,( which is essentially the HTML DOM (document object model) ), then your document hasn’t loaded yet and your Javascript might not work correctly.


In order to over come this problem, jQuery gives you a basic construct the - ready event which looks like



$(document).ready(function(){
//Your Javascript code here..
});



Let’s now quickly try an example,



<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Basics</title>
</head>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script>
$(document).ready(function(){
alert("document has loaded, you can execute your js here..!");
});
});
</script>
</body>
</html>



First thing to note is that above example links to Google’s CDN to load the jQuery core file. You can download the latest version of this file from the jQuery website.Here, the alert statement is guaranteed to work only after the document has finished loading.


jQuery CSS example



<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Basics</title>
<style>
label.test {font-weight: bold;}
</style>
</head>
<body>
<label>This is a label</label>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("label").addClass("test");
});
</script>
</body>
</html>



Here, we use the .addClass method to the label element to add a css class. Notice that this code is placed inside the $(document).ready(function(){}); To remove the class ‘test’ for the label element, replace the addClass(“"test”) with the removeClass(“test”);

jQuery Effects example



<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Basics</title>
</head>
<body>
<labe>This is a label</label>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("label").click(function(){
$(this).hide("slow");
});
});
</script>
</body>
</html>

Click on the label and notice that it slows disappears.




jQuery Callback example


The syntax for executing a callback with no arguments



$.get('myhtmlpage.html',myCallBack);



Here, the function myCallBack() is executed after the page ‘myhtmlpage.html’ is got from the server.


What the above Javascript code is doing is, It’s registering the function ‘myCallBack’ (notice that when you write it, it’s without () ), to the get event. i.e ‘myCallBack’ will be executed after the get is done.


 


Now, Consider a scenario wherein you need to register a callback function by passing arguments.


Do not try doing this, this is Wrong



$.get('myhtmlpage.html', myCallBack(param1, param2));



The above line won’t work because, myCallBack(param1,param2) is executed before it’s registered. In other words, The returned value of myCallBack(param1, param2) is registered as a callback function, which is not write.Javascript is expecting a function pointer here. so, the right way to do this will be to enclose  myCallBack(param1, param2) with function(){ }. Yes, you guessed it, that’s the anonymous function. (I’m really really found of this Javascript feature, sometimes it makes me think, that’s where languages like C# got the ideas from, C++ is an obvious choice Smile with tongue out)



$.get('myhtmlpage.html', function(){
myCallBack(param1, param2);
});

In the above example, an anonymous function is created (just a block of statements) and is registered as the callback function. Note the use of 'function(){'. The anonymous function does exactly one thing: calls myCallBack, with the values of param1 and param2 in the outer scope.


I’ll soon be writing more on jQuery and jQuery UI which i’ve started to use extensively off late. The following is worth reading for more.





Next Steps






















Thursday, 27 November 2008

DI Management Cryptography

This page contains useful free cryptographic software code that David Ireland has written or adapted in Visual Basic and ANSI C. It's updated frequently, so keep checking.

http://www.di-mgt.com.au/crypto.html

Wednesday, 26 November 2008

Tools for everyday development


1. Process Explorer
processexplorer
Process Explorer shows you information about which handles and DLLs processes have opened or loaded.
The Process Explorer display consists of two sub-windows. The top window always shows a list of the currently active processes, including the names of their owning accounts, whereas the information displayed in the bottom window depends on the mode that Process Explorer is in: if it is in handle mode you'll see the handles that the process selected in the top window has opened; if Process Explorer is in DLL mode you'll see the DLLs and memory-mapped files that the process has loaded. Process Explorer also has a powerful search capability that will quickly show you which processes have particular handles opened or DLLs loaded.

2. Debug View
debugview
DebugView is an application that lets you monitor debug output on your local system, or any computer on the network that you can reach via TCP/IP. It is capable of displaying both kernel-mode and Win32 debug output, so you don't need a debugger to catch the debug output your applications or device drivers generate, nor do you need to modify your applications or drivers to use non-standard debug output APIs.
Cool Features
  • Win32 OutputDebugString
  • Kernel-mode DbgPrint
  • All kernel-mode variants of DbgPrint implemented in Windows XP and Server 2003
DebugView also extracts kernel-mode debug output generated before a crash from Window's 2000/XP crash dump files if DebugView was capturing at the time of the crash.
3. PsKill
Windows NT/2000 does not come with a command-line 'kill' utility. You can get one in the Windows NT or Win2K Resource Kit, but the kit's utility can only terminate processes on the local computer. PsKill is a kill utility that not only does what the Resource Kit's version does, but can also kill processes on remote systems. You don't even have to install a client on the target computer to use PsKill to terminate a remote process.
P.S: Filemon and Regmon have been replaced by Process Monitor on versions of Windows starting with Windows 2000 SP4, Windows XP SP2, Windows Server 2003 SP1, and Windows Vista. Filemon and Regmon remain for legacy operating system support, including Windows 9x.
4. Resource Refactor
RefactoringTool-Menu
Features for Resource Refactoring Tool
  • Works with C#, VB.Net languages. Supports all project types that ships with Visual Studio 2005 including web sites and web application projects.
  • A preview window to show changes.
  • Finds other instances of the text being replaced in the project automatically.
  • Lists existing resources by their similarity level to the text being replaced.
  • Automatically replaces hard coded string with a reference to resource entry.
5. Ghost Doc
Automatically generates documentation for constructors,methods,properties and class. Integrates will with visual studio and style cop and is a must have to get rid of those style cop warnings.
6. Microsoft StyleCop
StyleCop analyzes C# source code to enforce a set of style and consistency rules. It can be run from inside of Visual Studio or integrated into an MSBuild project. This is a definite must have. It enforces consistent looking code across the whole project with multiple developers with varied tastes and preferences.
7. Microsoft FxCop
FxCop is an application that analyzes managed code assemblies (code that targets the .NET Framework common language runtime) and reports information about the assemblies, such as possible design, localization, performance, and security improvements. Many of the issues concern violations of the programming and design rules set forth in the Design Guidelines for Class Library Developers, which are the Microsoft guidelines for writing robust and easily maintainable code by using the .NET Framework.
FxCop is intended for class library developers. However, anyone creating applications that should comply with the .NET Framework best practices will benefit. FxCop is also useful as an educational tool for people who are new to the .NET Framework or who are unfamiliar with the .NET Framework Design Guidelines.
FxCop is designed to be fully integrated into the software development cycle and is distributed as both a fully featured application that has a graphical user interface (FxCop.exe) for interactive work, and a command-line tool (FxCopCmd.exe) suited for use as part of automated build processes or integrated with Microsoft Visual Studio® .NET as an external tool.

Links
Sysinternals Suite
FxCop
The Visual Studio Code Analysis Team Blog
StyleCop
Introducing StyleCop on legacy projects
Ghost Doc
Resource Refactor

Thursday, 20 November 2008

Unit Testing Silverlight Apps

Inarguably SL2 applications are  a whole new experience of developing cross platform applications from managed code environments.

Unit Testing becomes very vital while developing for cross browser enterprise class applications. Visual Studio's tight integration of the Unit Testing framework is such an invaluable tool in the code production pipeline. Now, with the release of a unit testing framework for SL2 applications, the same experience can be leveraged.  SL2 applications can be developed in a test driven fashion now. It perfectly suits an agile team. A team which is constantly driven by change and is delivering on a constant basis, almost every day in some cases. Much like a team which does not have a release model. (Check out Jeff Wilcox's blog). I guess the Silverlight test framework was itself developed using an agile methodology.

The SL Unit testing framework has  proved to be an invaluable tool personally for me. It allows me to test SL applications on 3 different browsers (cheap guess if you are thinking which browsers ) even though i target IE mainly. :P.  I'll write about it with a test application soon in my next blog. Till then, check out these links..

Some Links:

Silverlight Toolkit - You can find components like dockpanel, treeview etc for SL 2, themes and of course the source code for controls with unit tests and the unit test framework itself.

Jeff Wilcox - The guy who develops actively on the SL unit test framework at MS.

Scott Gu's small demo program on SL unit testing - (If you don't know this man, you are not worth living, jump off a building NOW!)

MS Silverlight Unit Test Framework - MS unit test framework for SL 2.

Wednesday, 19 November 2008