{"id":885,"date":"2010-01-03T22:15:08","date_gmt":"2010-01-04T03:15:08","guid":{"rendered":"http:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/885"},"modified":"2010-01-03T22:50:04","modified_gmt":"2010-01-04T03:50:04","slug":"microsoft-ajax-library-declarative-command-alternatives","status":"publish","type":"post","link":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/885","title":{"rendered":"Microsoft Ajax Library Declarative Command Alternatives"},"content":{"rendered":"
There may be another way to accomplish this, but I wanted to have a simple commanding system in the Microsoft Ajax Library<\/a> which worked outside of the templates. <\/p>\n After a number of false starts and frustration brought about by very limited documentation, I discovered a reasonable implementation.<\/p>\n I created a script file, \u201cCommanding.js\u201d (in a Scripts folder):<\/p>\n <\/a><\/p>\n 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\u2019re raised.<\/p>\n In the body of the HTML, I attached an instance of the \u201cWiredPrairie.Commanding\u201d 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\u2019ve used the special {{ }} syntax to indicate I want JavaScript code to execute when the event is raised. <\/p>\n <\/a><\/p>\n I needed my JavaScript WiredPrairie.Commanding class to load and run at the right time on the page, so I\u2019ve used the new loader classes in the Ajax library.<\/p>\n <\/a><\/p>\n First, I needed to declare this new script file and any dependencies. Since I don\u2019t yet have a minified version, I\u2019ve 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 \u201cComponentModel\u201d 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).<\/p>\n Next, I added code to indicate to the loader which scripts were necessary and let it determine the best way to load them:<\/p>\n <\/a><\/p>\n Here, you see the \u201cCommanding\u201d key is added to a special namespace, \u201cSys.scripts.\u201d<\/p>\n In the new onReady event which is raised when the DOM and scripts have been loaded, I\u2019ve added code to activate the control class I wrote:<\/p>\n <\/a><\/p>\n Finally, I wired up that event declared above in the onbubbleevent attribute on the body element.<\/p>\n <\/a><\/p>\n OK, that\u2019s 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:<\/p>\n\/\/\/ <reference name="MicrosoftAjax.js"\/>\n\n<\/span>Type.registerNamespace("WiredPrairie"<\/span>);\n\nWiredPrairie.Commanding = function<\/span>(element) {\n WiredPrairie.Commanding.initializeBase(this<\/span>, [element]);\n}\n\nWiredPrairie.Commanding.prototype = {\n initialize: function<\/span>() {\n WiredPrairie.Commanding.callBaseMethod(this<\/span>, 'initialize'<\/span>);\n\n \/\/ Add custom initialization here\n <\/span>},\n dispose: function<\/span>() {\n \/\/Add custom dispose actions here\n <\/span>WiredPrairie.Commanding.callBaseMethod(this<\/span>, 'dispose'<\/span>);\n }\n}\n\nWiredPrairie.Commanding.registerClass('WiredPrairie.Commanding'<\/span>, Sys.UI.Control);\n\nif <\/span>(typeof <\/span>(Sys) !== 'undefined'<\/span>) Sys.Application.notifyScriptLoaded();<\/pre>\n
<<\/span>body xmlns<\/span>:<\/span>sys<\/span>="javascript:Sys" \n <\/span>xmlns<\/span>:<\/span>wpc<\/span>="javascript:WiredPrairie.Commanding" \n <\/span>sys<\/span>:<\/span>attach<\/span>="wpc" \n <\/span>wpc<\/span>:<\/span>onbubbleevent<\/span>="{{onCommand}}" >\n <<\/span>div <\/span>>\n <<\/span>button sys<\/span>:<\/span>command<\/span>="startRunningCommand" <\/span>sys<\/span>:<\/span>commandargument<\/span>="iargueaboutit"><\/span>Run<\/<\/span>button<\/span>>\n <\/<\/span>div<\/span>>\n<\/<\/span>body<\/span>>\n<\/span><\/pre>\n
Sys.loader.defineScripts(null<\/span>, [\n{\n name: "Commanding"\n <\/span>,releaseUrl: "Scripts\/Commanding.js"\n <\/span>,debugUrl: "Scripts\/Commanding.js"\n <\/span>,dependencies: ["ComponentModel"<\/span>]\n }\n ]);<\/pre>\n
Sys.require<\/span>([Sys.components.dataView, Sys.scripts.jQuery,
Sys.scripts.Commanding]);<\/pre>\nSys.onReady(function<\/span>() {\n Sys.activateElements(document.documentElement); \n});<\/pre>\n
function <\/span>onCommand(sender, args) {\n if <\/span>(typeof <\/span>(args) !== "undefined"<\/span>) {\n var <\/span>commandName = args.get_commandName();\n var <\/span>commandArgument = args.get_commandArgument();\n alert(commandName + " " <\/span>+ commandArgument);\n }\n}<\/pre>\n
WiredPrairie.Commanding.raiseCommand = function<\/span>(sender, commandName, commandArgument, commandSource) {\n var <\/span>source = sender || document.body;\n Sys.UI.DomElement.raiseBubbleEvent(source, new <\/span>Sys.CommandEventArgs(commandName, commandArgument, commandSource));\n}\n\nvar <\/span>$sendCommand = WiredPrairie.Commanding.raiseCommand;<\/pre>\n