Xml-script: how to use your custom classes from it
Today I've built a simple custom Action so that I could see what is needed to use a custom element from xml-script. So I started with something like this:
Type.registerNamespace( "LA" );
LA.MyAction = function(){
//code...doesn't really matter
}
//more prototype code which really doesn't matter :)
LA.MyAction.registerClass( "LA.MyAction", Sys.Preview.Action );
It was time to start writing xml-script...Though I really knew it didn't work, I started by trying something like this:
<script type="text/xml-script">
<page xmlns:script="http://schemas.microsoft.com/xml-script/2005">
<components>
<button id="bt">
<click>
<MyAction>
...
</MyAction>
</click>
</button>
</components>
</page>
</script>
Yes, it didn't work because the parser was looking for the MyAction class in the default namespaces added by the AJAX client bits. I though that there should be a way to associate a xml namespace with the namespace used in Javascript where my class was defined. After taking a peek at the previewscript.js, I didn't found any method which registered a namespace. So, I decided to look at the _processPrefixMapping method of the Sys.Preview.MarkupParser to see what was going on during xml namepsace resolution. As i've already said, when you use the default namespaces, you automatically get the namespace list defined by the AJAX bits; If you're using a different namespace, then you must make sure that the value you use for the xml namespace is the same you've used for the code namespace (and please pay attention to case-sensitivity because it's important here). So, that means that in order for my code to be correctly interpreted, you need to do this:
<script type="text/xml-script">
<page xmlns:script="http://schemas.microsoft.com/xml-script/2005" xmlns:test="LA">
<components>
<button id="bt">
<click>
<test:MyAction>
...
</test:MyAction>
</click>
</button>
</components>
</page>
</script>
And now the parser is happy and able to instantiate my custom action. There are some remarks about this strategy:
- I understand why they do that, though I'd really prefer to be able to associate a specific xml namespace to a code namespace. In other words, I'd prefer to call a register method so that I could say: "map http://mydomain.com" to namespace X and namespace Y"
- If you really want to go this way (ie, use the same name for a code namespace and a xml namespace), then one of things must be done: make element names case sensitive too (currently, you only need to write the name of the class, without paying any attention to capital letters) or remove that restriction from domain registration
So, it looks like today xml-script is still sucking a little bit :) And what do you think?