September 2007 - Posts

This post is my english translation of the italian version I posted some days ago. Hoping it may be better than the automated Italian to English translation from google. I have not merely translates word-by-word the previous post but I have completely rewritten the post.

Whoever had ever written web applications know that scripting code has the disagreeable habit to make the markup plenty of code and cross references that make page hard to read and maintain. So, i decided to write this post explainig the way I recently adopted to strip out the biggest part of this scripting code infesting my pages. So here is the title of this post, that wants to teach a better way to organize our scripting code by creating a

I had this idea working with Microsoft AJAX Library, I tried to implement it during my working time, end it become my only way to work because it permit to separate interface from logic as many code guru explains from many years. The base of this idea is a little class, written using the library that I use as base class for every page I make.

1 Type.registerNamespace('Elite'); 2 3 Elite.Page = function() 4 { 5 this.onload$delegate = Function.createDelegate( 6 this, 7 function(eventData) 8 { this.load(); }); 9 10 this.onunload$delegate = Function.createDelegate( 11 this, 12 function(eventData) 13 { this.unload(); }); 14 15 window.onload = this.onload$delegate; 16 window.onunload = this.onunload$delegate; 17 18 Elite.Page.initializeBase(this, [window]); 19 } 20 21 Elite.Page.prototype = 22 { 23 initialize : function() 24 { 25 Elite.Page.callBaseMethod(this, 'initialize'); 26 }, 27 load : function() 28 { }, 29 unload : function() 30 { }, 31 dispose : function() 32 { 33 if (this.onload$delegate) delete this.onload$delegate; 34 if (this.onunload$delegate) delete this.onunload$delegate; 35 36 Elite.Page.callBaseMethod(this, 'dispose'); 37 } 38 } 39 } 40 41 Elite.Page.registerClass('Elite.Page', Sys.UI.Control);

This class do some simply things I will use every time I devolp a page and grant me the capability to approach the web page development every time in the same way, in a similar way like when i write an ASP.NET codebehind page. The class attachs the onload and onunload events and define also a load() and unload() method called every time this events happen. So like a common ASP.NET page it will have initialize, load, unload and an addictional dispose method useful for deallocating resources like event handler that waste of memory leaks the browsers. When we will write the page class it will b really important to call the bae method every time we override oneof these methods.

Here is how to work with this class when creating a page:

1) first of all we have to create a codebehind file ina a way similar to visual studio behavior. Giving a page called dashboard.aspx I will create a javascript codebehind called dashboard.aspx.js. I simply put this file side by side with the page because when I deploy the application tipically I make this scripts embedded resources and will load them using webresource.axd directly frm the web application assembly (giving you use web application project). This file will contains a class extending my Elite.Page class.

2) now it is the time to include the javascript file with the "Elite.Page" class definition (I call it page.js). It is rquired also you include the Microsoft AJAX Library so if you are developing an ASP.NET page probably the better things todo is to reference this file into the ScriptManager <scripts> collection.

3) after setting up the files in the directory, it is now tim to implements the class specific for our page. I will name it always with the same name I use for the C# codebehind class. Here is a sample:

1 Type.registerNamespace('MyApplication'); 2 3 MyApplication.DashboardPage = function() 4 { 5 MyApplication.DashboardPage.initializeBase(this, [window]); 6 } 7 MyApplication.DashboardPage.prototype = 8 { 9 load : function() 10 { 11 this.bSave = $get('bSave'); 12 MyApplication.DashboardPage.callBaseMethod(this, 'load'); 13 } 14 }; 15 16 MyApplication.DashboardPage.registerClass('MyApplication.DashboardPage', Elite.Page);

In this sample you may see the right place how to "register" controls you will use later in the page lifetime. As you may see in the load() event handler I search for a control named "bSave" and record it into a variable with the same name. Here I can also attach event handlers (e.g. the onclick button) to handler user inputs. To take a rference you will use $get()

4) To reference the class, you have to push a little script into the startup sequence. I usually use the ScritpManager.RegisterStartupScript() to insert this simple line:

var currentPage = $create(MyApplication.DashboardPage, {}, {}, null, null);

Calling this method you simply take an instance of the class. It will also call the initialize method starting the page lifecycle. You may manage events during this cycle in the same way you do with ASP.NET pages. There was only a little trouble. All you know that ASP.NET pages create a composite id for WebControls. So to take references to this objects you will have to pass this id from the ASP.NET server-side code to the client-side javascript class using the ClientID property. I have a small idea incubating... but this will be argument for a future post.

This time I decided to write my post - on Microsoft AJAX Library - in my italian blog. Of course I will translate in english as soon as possible but I included a link to my post translated by google for english reading people. This post explain a tecnique to encapsulate all the Javascript code of a single page in a class. This may grant you the ability to strip out all Javascript code from the markup code. This is really a good result to improve maintenance of pages.

Original Post: Una classe Javascript per le pagine... ovvero, come organizzare meglio i propri script

Translated Page: A Javascript for your pages…

Recently I've developed my photoblog using Silverlight 1.0 and I found some trouble working hardly with animations. My concern was the coordination of different animation running in a scene. I found often difficult to create sequences of animations where a storyboard start running when another storyboard ends. After few tries I decided to write some Javascript classes emulation the animation model exposed by AJAX Control Toolkit where there are also some classes capable of run parallel, and sequence animations simply by declaring them and call the run method.

I decided to publish here the result of my work. I created a small hierarchy starting by Elite.Animatable class that act as abstract base class for all other types. Then I implemented some times this base creating this classes:

  • Elite.Animation :
    • this class is the starting brick of the animation system. It accept a storyboard as input and control it by begin() and stop() method.
  • Elite.CompositeAnimation
    • this class work as base for animations that accept multiple storyboard as input. It extends Elite.Animatable 
  • Elite.SequenceAnimation
  • Elite.ParallelAnimation
  • Elite.ConditionalAnimation
    • this classes represent different behaviors for multiple animations. Them accept an array of Elite.Animatable and run according to his name.

With this hierarchy is possible to write something like this code:

var growth = new Elite.Animation(this._root.findName("growth"));
var translate = new Elite.Animation(this._root.findName("translate"));
var toYellow = new Elite.Animation(this._root.findName("toYellow"));
var toRed = new Elite.Animation(this._root.findName("toRed"));
var toBlue = new Elite.Animation(this._root.findName("toBlue"));
var parallel0 = new Elite.ParallelAnimation([translate, toYellow]);
var parallel1 = new Elite.ConditionalAnimation([parallel0, toRed, toBlue]);
parallel1.add_decisionRequest(
    function(sender, args)
    {
        var i = parseInt( Math.random() * 3 );
        args.set_animationIndex(i);
    });

var fullAnimation = new Elite.SequenceAnimation([growth, parallel1]);
fullAnimation.begin();

In the declarations the storyboards are wrapped from a simple Animation object. Then the animatables are composed into a ParallelAnimation and a ConditionalAnimation. This are also wraper in a SequenceAnimation. When the begin method of the SequenceAnimation will be called the aniamtables object will run sequentially, parallel or conditional composing a full animation. The mean of the composite animation are:

  • Parallel: all the animatables run parallel starting at the same time. A "completed" event will be fired when the last animatable ends.
  • Sequence: the animatables run one by one. The second run when the first stop. A "completed" event will be fired when the last animatable ends.
  • Conditional: when the animation begin a DecisionRequest event fire and the event handler must set the index of the animatables to start into his array.

The powerful aspect of this library is the classes architecture. All the classes extendes Elite.Animatable and all te classes accept instances of Elite.Animatable as input. So you can compose multiple Animatables one inside the other. Sequence into Parallel, Parallel into Conditional, etc... in this way you may obtain easily every animation behavior.

The downloadable archive contains a visual studio project with the animation.js library (also in debug version) and some sample code.

Download: Elite.Silverlight.Animation.zip (239KB)

I post to my weblog from about 4 years but I never imagined one day I will start blogging in a language differen from Italian. Like you may read my english speaking i not really fine, but I hope it will be better with continuos writing. My name in Andrea Boschin, and from a few months I become a Most Valuable Professional on ASP.NET technologies. During this years I have not only blogged. My primary occupation is to work on web applications during the daytime and to manage an italian user group named XeDotNet, try new technologies an write about them during my free time, specially by night.

When I saw the announce about new open positions on http://weblogs.asp.net I know the time to begin a new weblog in english, has come and I answer to his post. Now I'm here. There are many samples about ASP.NET I would like to write in this pages, and i hope to begin soon to post my night thought. Probably I begin to write my post twice, one time in Italian and one time in english, but about this thing I still have to think to take a decision. Waiting for my next issue probably you would like to have a look at my Italian blog, so if you try something interesting I may starts with this argument instead of other.

More Posts