Microsoft Ajax Library Declarative Command Alternatives

There may be another way to accomplish this, but I wanted to have a simple commanding system in the Microsoft Ajax Library which worked outside of the templates.

After a number of false starts and frustration brought about by very limited documentation, I discovered a reasonable implementation.

I created a script file, “Commanding.js” (in a Scripts folder):

/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("WiredPrairie");

WiredPrairie.Commanding = function(element) {
    WiredPrairie.Commanding.initializeBase(this, [element]);
}

WiredPrairie.Commanding.prototype = {
    initialize: function() {
        WiredPrairie.Commanding.callBaseMethod(this, 'initialize');

        // Add custom initialization here
    },
    dispose: function() {
        //Add custom dispose actions here
        WiredPrairie.Commanding.callBaseMethod(this, 'dispose');
    }
}

WiredPrairie.Commanding.registerClass('WiredPrairie.Commanding', Sys.UI.Control);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

This is just a basic JavaScript class, with the default implementation provided by the Ajax Client Control template in Visual Studio 2008. The reason this is needed is that Controls all support a special event onBubbleEvent that is needed by commands when they’re raised.

In the body of the HTML, I attached an instance of the “WiredPrairie.Commanding” class to the body. Next, I attached a JavaScript function to the onbubbleevent (wpc:onbubbleevent, remember that all attributes need to be all lowercase). Here, I’ve used the special {{ }} syntax to indicate I want JavaScript code to execute when the event is raised.

<body xmlns:sys="javascript:Sys" 
    xmlns:wpc="javascript:WiredPrairie.Commanding" 
    sys:attach="wpc" 
    wpc:onbubbleevent="{{onCommand}}" >
    <div >
        <button sys:command="startRunningCommand" sys:commandargument="iargueaboutit">Run</button>
    </div>
</body>

I needed my JavaScript WiredPrairie.Commanding class to load and run at the right time on the page, so I’ve used the new loader classes in the Ajax library.

Sys.loader.defineScripts(null, [
{
    name: "Commanding"
    ,releaseUrl: "Scripts/Commanding.js"
    ,debugUrl: "Scripts/Commanding.js"
    ,dependencies: ["ComponentModel"]
 }
 ]);

First, I needed to declare this new script file and any dependencies. Since I don’t yet have a minified version, I’ve just specified the same file for the releaseUrl and the debugUrl. For the dependencies property, I looked through a few source files to discover that the “ComponentModel” key included Sys.UI.Control, which my class depends on to be created, so I added that here (hopefully this will be documented at some point).

Next, I added code to indicate to the loader which scripts were necessary and let it determine the best way to load them:

Sys.require([Sys.components.dataView, Sys.scripts.jQuery,
Sys.scripts.Commanding]);

Here, you see the “Commanding” key is added to a special namespace, “Sys.scripts.”

In the new onReady event which is raised when the DOM and scripts have been loaded, I’ve added code to activate the control class I wrote:

Sys.onReady(function() {
    Sys.activateElements(document.documentElement);    
});

Finally, I wired up that event declared above in the onbubbleevent attribute on the body element.

function onCommand(sender, args) {
    if (typeof (args) !== "undefined") {
        var commandName = args.get_commandName();
        var commandArgument = args.get_commandArgument();
        alert(commandName + " " + commandArgument);
    }
}

OK, that’s cool, but you might want to raise a command without it being triggered by a control event (such as a click). So, I added a simple function to my Commanding class:

WiredPrairie.Commanding.raiseCommand = function(sender, commandName, commandArgument, commandSource) {
    var source = sender || document.body;
    Sys.UI.DomElement.raiseBubbleEvent(source, new Sys.CommandEventArgs(commandName, commandArgument, commandSource));
}

var $sendCommand = WiredPrairie.Commanding.raiseCommand;

Because of the way the raiseBubbleEvent works, it does depend on the source being set to a valid control/element (as raiseBubbleEvent walks through the parent chain until there aren’t any more parents) – in this case, I’ve defaulted to the body element.

Finally, an enhancement to the test case above to demonstrate both receiving and sending a command:

function onCommand(sender, args) {
    if (typeof (args) !== "undefined") {
        var commandName = args.get_commandName();
        var commandArgument = args.get_commandArgument();
        switch (commandName) {
            case "startRunningCommand":
                $sendCommand(null, "alertCommand", new Date().toLocaleTimeString(), null);
                break;
            case "alertCommand":
                alert(commandArgument);
                break;
            default:
                break;
        }
    }
}

One command just sends another command which displays an alert. It’s simple, but functional.

Enjoy.

How to make your e-mail look like Spam, #221

Here’s an e-mail I received this morning, a legitimate e-mail:

image 

“Travel Planner Email”, from “Airlines”

????

Huh?

We’re planning a brief vacation that requires a bit of air travel, so before I hit delete, I paused to see what the spam e-mail said – and then realized it was not spam at all.

Note to programmers: ….

Aw heck.

Just stop doing stupid anti-human stuff.

Time for JavaScript.NEXT

OK, I’ll admit that HTML 5, when it becomes a true standard and the common browsers implement it and all of CSS 3 that we won’t need as many browser plug-ins for doing rich interactive applications anymore. We could ditch Microsoft’s Silverlight and Adobe’s Flash and not look back.

But, to do that, we need to move the web to a better programming language!

Reading this rant from MG Siegler on Tech Crunch made me want to come clean about my true feelings.

As Google Backs Away From A Plug-in, Microsoft Rushes Towards One

I don’t want to need Flash or Silverlight.

But, I’ll take either of them over programming a large scale web application using JavaScript – since both of them have a modern programming language (ActionScript 3 and C#). Seriously. JavaScript, born in 1995. It’s 14 freaking years old as of this December (as it was first released in December 1995). We’ve got budding programmers who were born AFTER it was created. JavaScript, unlike many of it’s commonly used, similarly aged counterparts, has not had any significant upgrades or improvements to reflect new market needs. Java and C++ both have (Java got generics for example), and C# was born in the last decade. I like Silverlight more than Flash only because of the programming language (and IDE) attached with its development.

Here’s my short list of complaints about JavaScript:

  • Dynamic typing – great, I can’t know until runtime what a type should be, and even then it could change. Makes for a great time.
  • Prototype – oh don’t get me started on the patterns that are necessary to create a robust object oriented framework in JavaScript – and “this”, and “oops, you’re missing a comma, good luck finding that!”
  • Closures – the source of too many memory leaks and confusion over the years, especially given the subtly of its behavior in JavaScript
  • No official “imports” or “usings” to make doing code-completion practical, and one JavaScript file can’t declare another JavaScript file as a dependency and have it automatically be loaded (just once of course), without creating a new framework.
  • Run-time code discovery – it’s great to be able to load things dynamically, but it’s completely a mess at design time trying to sort everything out and make sense of a large application.
  • Debugging – terrible IDE and debugging support across the browsers. Too many “alert” style debugging workflows in this day and age.
  • Interpreted – still making the browser do extra work and a bonus feature: you don’t know about coding errors until you try to execute the code!
  • Poor internationalization support – to do it right, you need to build functions or get a library to parse and format properly

(Don’t get me started about the inconsistency of DOM access from JavaScript across browsers – which I realize isn’t JavaScript – it’s a browser thing – but they’re intertwined from all practical purposes).

(And, I’m not complaining about a web application that consists of 2 JavaScript files, where one of them is JQuery. Think big. Applications. Complex workflows. Frameworks. Etc. It’s tough stuff. It’s doable, but unnecessarily difficult).

Major Web applications would have taken off long ago if the entire development experience/environment would have modeled after something like Visual Basic 3, 4, 5,  or 6. Web applications are still held back by an awful development experience.

Do you love JavaScript? Does it need to be upgraded to this century?

Updated to reflect a bit of ranting inconsistency.

Finally, someone admits that Silverlight and WPF will (likely) converge

However, will Silverlight and WPF merge?

From Pete Brown:

The Future of Client App Dev : WPF and Silverlight Convergence

As a Microsoft employee (Developer Community Program Manager for Windows Client), he said:

What the Future Holds – Convergence of WPF and Silverlight

 

I recently spoke with Ian Ellison-Taylor at Microsoft. Ian is a General Manager at Microsoft, reporting directly to Scott Guthrie. Among other things, his group handles both Silverlight and WPF (and RIA services and a lot of other stuff). I figured if I wanted the skinny on the future, he’d be the guy to talk to. So, he and I talked about the convergence of Silverlight and WPF, and later exchanged some email on the topic.

 

In the future, it is very likely that both Silverlight and WPF will be a single technology with a single codebase. After all, Silverlight was originally known as WPF/E (E as in Everywhere), and in an amazing 180 degree reversal of our usual approach, we took an ugly codename and created a great product name (Silverlight) from it.

D’uh. Like it wasn’t obvious, especially with the explosive growth and capabilities of Silverlight, why wouldn’t this be on the road map?

Of course, there’s no time frame mentioned, so don’t plan on your Silverlight application being able to use real 3D and actual DataTriggers any time soon.

Impressive Silverlight game engine? Or something else entirely….?

A game in Silverlight – since I can’t read Chinese, I’m not sure what it’s all about, but it looks decent, and doesn’t seem to spike my CPU when playing.

I don’t understand the point of the demo/game that’s there and it seems to be impossibly difficult (as you can see from the screen shot).

Try it here:

http://silverfuture.cn/

image

Found via Silverlike.

As best as I can determine, here are the functions of the various features along the bottom:

image

Update: The comments have a better explanation of what the various items along the bottom do in the “game.”