Sep 23 2010

Inspire the web with just 10K!

Category: NovoGeek @ 19:02

That was the caption of the 10K coding challenge hosted by An Event Apart, in association with MIX Online. The challenge is to build a web app in less than 10 KiloBytes. The rules are:

  • Total file size, including images, scripts & markup, can’t be over 10K.
  • Apps should work equally well in IE9 Dev preview, Firefox and webkit browsers. HTML5 can be used.
  • jQuery/Prototype/Typekit can be used and they won't be counted against 10K.

It is really amazing to see excellent hacks written under just 10K. One of my favorite geeks from Yahoo, Christian Heilman wrote a World Info hack using free services under 4.83 KB!! It fetches list of all countries in the world & on selection it will fetch the country's map, wikipedia info n stuff. Interesting..isnt it? There are around 400 of interesting ideas/hacks submitted, which are a source of great learning. Most of the prize winners are smart coders of JS based games.

My 10K Hack:

I have submitted a quick, dirty hack - 10K Start page. The idea is fairly simple, to have an ajax start page like iGoogle, PageFlakes, NetVibes but under 10K. Though it is not possible to add all the modules and beautify like the big guys did, I tried to have maximum possible features under 10K. I have used jQuery, HTML5, Yahoo Query Language (YQL), CSS3 to build the app. The app has 4 widgets- Twitter search, Yahoo weather search widget, sticky notes widget and a generic RSS reader widget. New widgets can be added, removed, dragged & dropped.

YQL queries: Thanks to Yahoo! For sure, I cannot think of a way other than YQL to build a mashup so quickly. I used JSON format so that parsing on JS would be optimal and easier. Here are the YQL queries which I used:

Twitter search widget: Click here
Yahoo weather widget: Click here
RSS widget: Click here

So, for the above 3 widgets, all my JS codes involves fetching data from these YQL services in JSON format, parsing them and displaying in corresponding divs using jQuery. 

HTML5 features:

ContentEditable: For sticky notes widget, I just placed a div tag like this: '<div contenteditable="true" class="sticky">Edit this sticky notes...</div>'  The "contenteditable" attribute is a new attribute defined in HTML5 specs, which makes a div editable on click and readonly on mouse out.

LocalStorage: It is a client side key value database, which can be used to store data in user's browser. I'm using this to save the layout on "window.onbeforeunload" event and restore the layout on pageload.

Adding/Removing/Draging widgets: All these operations are simple jQuery based DOM manipulations. For drag & drop, I'm using jQuery's jqDnR plugin.

Participating in such events is real fun, as we don't often try to think in terms of minimalistic code.At the last minute, I had to even do the optimizations like renaming "jQuery.js" to "j.js", just to reduce 5 bytes!! 

Have any suggestions/comments? Please let me know.

 

Tags: , , , ,

Aug 8 2010

jQuerify your ASP.NET apps - Microsoft Community Tech Days 2010

Category: NovoGeek @ 18:16

Happy to say that I have presented at Microsoft Community Tech Days 2010 on "jQuerify your ASP.NET apps" at Microsoft Hyderabad. The event, organized by Microsoft User Group Hyderabad (MUGH), had 400+ audience in Developer and IT Pro track!!

jQuery based PPT

Presentation: Click here

Demos:  Click here

I chose to prepare my presentation using jQuery itself, instead of power point, which would itself be a demo. The response from the audience was really encouragingSmile

Note: I used a lot of plugins in my demos but had to tweak them to meet my needs. So please don't use the versions which I am using in my demos as-is. Suggest you to download the original versions from the urls in the plugin files.

It was a great experience interacting with several enthusiastic developers and geeks. I've personally learnt a lot! Hope to meet you'll again. Please write to me in case of any doubts about using jQuery with ASP.NET

Happy coding Smile

Tags: , , ,

Aug 1 2010

Enhancing scalability & maintainability of your JavaScript/jQuery code – JS Design patterns

Category: NovoGeek @ 08:29

Are you building a huge, jQuery-ASP.NET web application, which is subjected to a lot of global code changes in your client script? If yes, you should probably consider the below design pattern, which would help you have a better control on code execution.

Maintainability problem with large chunks of JavaScript code :

Imagine a web application having ~400 .js files. Each .js file would have code for AJAX calls/DOM manipulations for the respective .aspx page. But there would be some code which is redundant and can be moved to a common global function, which can be accessed by all .js files. With respect to ASP.NET paradigm, this can be compared to - your code behind partial class inheriting a base class. So whenever you have a global change, you can just change the base class and all .aspx files will have the changes.

However, there is a huge difference in the above comparison. ASP.NET has a powerful page life cycle which strictly defines which piece of code should be executed at what event. i.e., Pre Init, Init, Page Load, Pre Render etc. In the case of JavaScript, it is just dependent on ‘where you place your code’. i.e., even if you call a global function on line 1, developers can always add lines above and below it, which will mess up your code execution sequence.  This pattern is an attempt to have control on JS code execution, having Pre and Post events.

Solution- Using wrapper which can provide pre/post events:

Let’s say for every page, we need to prevent post back on click of any button. i.e., writing “e.preventDefault()” on button click events. I call this as a “Pre Event”, since this is to be executed before any click events are written.

Similarly, let’s say, for every page, we need to disable/show/hide certain controls, based on few conditions. I call this as a “Post Event”, since this code has to be executed at the end (similar to pre render event of asp.net model). So here is how we can try to enforce this.

Enhancing our chainable JavaScript library:

The following description is based on my previous article, building a chainable JavaScript library. For the global “Pre” and “Post” events, I’m going to extend the chainable JavaScript library with the “execute” method, like this:

(function(){
    var value='Hello world';
    var preInit=function(){
        console.log('pre Init');
    }   
    var preRender=function(){
        console.log('pre Render');
    }
    var mySpace=function(){
        return new PrivateSpace();
    }
    var PrivateSpace=function(){
 
    };    
    PrivateSpace.prototype={
        init:function(){
            console.log('init this:', this);
            return this;
        },
        ajax:function(){
            console.log('make ajax calls here');
            return this;
        },
        cache:function(){
            console.log('cache selectors here');
            return this;
        },
        setValue: function(newValue){
            value=newValue;
            return this;    
        },
        getValue: function(callbackFunc){
            callbackFunc.call(this,value);
            return this;    
        },
        execute:function(oldClass){
            preInit();
            var obj=new oldClass();
            obj.init.call(this);
            preRender();
        }
    }
    window.my$=mySpace();
})(); 

To explain the execute method, let’s consider the below code:

var CommonSearch=function(){
    var pageLoad=function(){
        console.log("page load operations here...");
    }
    return{
        init: pageLoad
    }
}

The above code is what resides in your ‘.js’ file. It is written in revealing module pattern. Here, CommonSearch is a constructor function and to execute it, we should say:

$(document).ready(function(){
    var objCommonSearch=new CommonSearch();
    objCommonSearch.init();
});

This does not have “Pre” and “Post” events. So if we need some global changes, we are lost! We have to manually change all 400 “.js” files. To troubleshoot this problem, if we use our my$.execute method, we can say:

$(document).ready(function(){
    my$.execute(CommonSearch);
});

Note that in the above snippet, we are not creating instance of CommonSearch constructor function. Instead, we are passing it to my$.execute, which internally calls the private “PreInit()” method first, then executes “init()” of  CommonSearch, and calls the private “preRender()” method. So, we are enforcing a particular sequence of code execution, which solves our problem.

[Note: As far as I know, 100% enforcement is impossible in JavaScript. If you are a JS Ninja, you can easily bypass whatever enforcement is set. That is the beauty of the language!!]

How this is different from using a common global function in document.ready, which can do the same functionality as my$.execute? Simple…we have good name spacing and chainability in our my$ function, which prevents conflicts with other modules.

If you are a JavaScript newbie, this article might sound a bit weird., but actually it is not so. I’m no JS guru to say this is the best approach. But this approach helped me a lot in scaling up easily and maintaining huge .js files, without any issues. If you can think of a better approach, please suggest. I’m waiting to learn..

Happy coding Smile

Tags: , ,