Dojo Toolkit

dojo/parser

Ok, what is becoming a bit of a trend, I am adding a third to my series on forthcoming features in Dojo core in 1.8.  I have already covered dojo/promise and dojo/request, now I am going to cover changes to dojo/parser.  This is something I am quite passionate about, since I had my hand directly involved in the features that were added, with lots of help from Bill Keese and Ben Hockey (and others).

With the full conversion to AMD in 1.7, there were likely a few features that made the declarative syntax a bit more challenging to use, versus the programmatic syntax.  While the programmatic approach to Dojo is usually a better way of creating applications, the declarative way is very popular with developers, great for prototyping and a quick way to get started with Dojo, especially when using the Dijit package of widgets.

For those not familiar with programmatic, versus declarative syntax, here is a quick example of creating a Dijit button and making it log something to the console when clicked.  First here is how we would do it programatically:

Now the same HTML snippet done declaratively:

The declarative syntax is then converted by the dojo/parser into instantiated widgets, essentially converting your decorated HTML code into programmatic code.  It does the heavy lifting for you, so you don’t have to keep managing the nodes in your document for all your widgets.

Now for the improvements in 1.8.  The first was that you could only specify the class in declarative syntax using a global class name (the old way, dot notation, e.g. dijit.form.Button).  With AMD and the deprecation of Dojo declaring classes in the global namespace, this was starting to become a problem.  So now the dojo/parser accepts the Module ID (MID) as the type (e.g. dijit/form/Button).  In fact this is now the preferred way of referring to a object type in declarative markup.  For example, our button from above should be:

The second added feature is to bring the declarative scripting fully capable with the introduction of dojo/aspect as the preferred way of modifying the behaviour of an instance in Dojo.  dojo/aspect supports the semantic concepts of before, around and after which are part of Aspect Oriented Programming (AOP).  Now the declarative scripting supports those as well, so you can do the following:

Now for the “cool” feature, but which could easily cause quite unintended consequences, is something called auto-require.  This caused a lot of debate within the Dojo developer community, mostly because you don’t have to be explicit about your requirements with your declarative scripting.  Basically if the dojo/parser encounters a MID that isn’t loaded, it will attempt to load it for you.  It makes things easier, but it can mask problems and may cause a lot of negative impact on performance when using built layers that may not contain all the right modules.  At the end of the day though we thought it was useful enough to be included.  Without it, you would have had to require all your modules used in your declarative markup before invoking the parser, which would have looked something like this for our button:

[notice]It is still better, since 1.7 to manually invoke the parser.parse() instead of using parseOnLoad in dojoConfig.[/notice]

Another new feature which fits well in the world of AMD is a declarative require.  This allows you to require in modules without having to do it within a JavaScript code block in your code.  What it essentially does, is allow you to load modules and map them into the global namespace.  A declarative require is a <script> block that contains a JavaScript object that defines the mapping of the modules and would look like this:

Because of the auto-require and declarative require use Dojo’s loader (via require()) and require() operates in an async fashion, it has meant that dojo/parser now operates in a async fashion.  Historically the .parse() function returned an array of instantiated widgets.  For backwards compatibility reasons .parse() still returns an array, but it also behaves like a promise too.  When the parser does not need to use auto-require or declarative require or Dojo loader is running in sync mode, this array will be populated with the instantiated objects.  When it is running in an async fashion, the array will be empty but the promise will be resolved with an array of objects.

For new development it is best to ensure you treat parser.parse() as if it is always running in an async fashion.  So if you need access to the instantiated objects or if you need to do something once the .parse() is finished, you should do something like this:

One piece that I am still working as I write this is to get the Dojo builder to be able to analyse your HTML resources for a build, understand the dependencies in the marked up code and allow you to build that into your built Dojo or a layer.  Hopefully this will help people avoid some of the issues that could occur if they just used features like auto-require without thinking about the consequences.

There is a lot of “good” stuff in the dojo/parser in 1.8 that makes it more “modern” like other parts of Dojo.  Hopefully for those who use it, these features will help you take advantage of a modern Dojo and feel “left behind”.

Dojo Toolkit

dojo/promise

It seems that my dojo/request article got a few views, so I figured I would take a moment to talk about dojo/promise, another major enhancement in the core of the Dojo Toolkit in 1.8.  Mark Wubben rewrote the promise API while keeping it compatible with the “modern” Dojo promise API.

Just a reminder, the “modern” promise API arrived in Dojo 1.5, where “callback chaining” and the promise was introduced.  Prior to that you had to use addCallback and addErrback when dealing with a Deferred object.  As of 1.5, the API changed so you could do something like this:

None of that changes for 1.8, but the underpinnings have totally changed and there is a more robust API for managing promises and futures.  In 1.8, the new abstract class of dojo/promise/Promise is introduced.  This provides the core API of Promises in Dojo and is what dojo/Deferred now implements.  Plus there are a couple of new syntactically named methods:

Dealing with multiple promises (like for example, making several requests to several services) and then doing something when one or all of them were resolved was dealt with dojo/DeferredList.  While that is still there (and still works), dojo/promise has introduced two new syntactically named modules dojo/promise/all and dojo/promise/first, which will roll up a bunch of promises and return you a new promise that could work something like this:

The last significant “new thing” in my opinion is that there is the dojo/promise/tracer.  What this does is essentially allow you to centrally manage the events of Promises.  What you do is load the module and then on any of your Promises/Deferred, turn on trace by calling the .trace() (or .traceRejected()):

So like dojo/promise, I think there are some really “cool” things coming in 1.8 in the “core” of Dojo.

Dojo Toolkit

dojo/request

In the forthcoming Dojo Toolkit 1.8, the dojo/request package, written by Bryan Forbes, has some really “cool” features in my opinion.  While the package “modernises” the Dojo IO/Request API, it also has some features that will really help end developers.

First, it is designed to Just Work™.  It works in a browser environment or a node.js environment seamlessly.  For example, if I wanted to load some JSON and do something with it, in a browser, I would do this:

But let’s say you were doing it on node.js. What would you need to do then? You wouldn’t do a thing. You would simply run your code with something like this node ./dojo/dojo.js load=example at a command line.  dojo/request will automatically figure out what platform you are on and load the appropriate request provider for you.

The second “cool” feature in my opinion is the ability to use the dojo/request/registry to “transparently” manage multiple request providers without having to worry about which one you are specifically using.  Just set it up and fire your requests away.  For example, take the situation that I would like to use JSONP some of the time and JSON the rest of the time (maybe I have a x-domain service where I have to use JSONP instead of just plain JSON).  Well, in order to do that, I would need to use dojo/request/script to get the JSONP, but I couldn’t use it to just fetch plain JSON and want to use dojo/request/xhr (which is the default provider for browsers).  In order to do that, I would do something like this:

Lastly, maybe I have some strange, bizarre encoding (or maybe I need to do some pre-transforms on my JSON or XML to properly convert them into objects).  Using dojo/request/handlers, I can add to the pre-existing handlers with my own, and then can use the handleAs property to to utilise my custom handler:

So while, Dojo Toolkit 1.8 will be a “stepping stone” to Dojo Toolkit 2.0, there are some really cool features that you can use now.

Marriage…

Kit and Simon

On the 6th December 2006 I entered into a civil partnership with Simon.  It was about a year after the UK made such an option open to us.  I encountered the immigration officer years later as I became an immigration advocate in the UK.  He indicated to me that my proposed civil partnership visa was the first one he had ever issued.  I am now a UK citizen and after having been “together” as a couple for 6 years, Simon and I have spent another 6 years with full legal benefits akin to marriage as we have lived in the UK.

While it wasn’t the only factor, the main factor is the reality that under current laws, Simon has no legal right to be in the USA.  We could have easily have settled in the US and continued down the path of being a net positive to the economy in the US.  No matter what the “sanctity of marriage” means, we have been a couple for well over a decade now.  We both earn substantially above average salaries and fully pay our taxes.  We don’t hold hands or kiss in public.  Very few people would assume we were a gay couple.  We live our lives for ourselves and make sure we contribute to society appropriately.  We are a positive influence on our family and friends.  On the 6 December 2006 the world did not stop rotating on its axis.  Society did not fall apart.  No one even noticed, except for the two of us our family and the lovely lady who was the Lambeth Registrar.

We are still ostracised from the US.  The North Carolina vote hurts.  It says to us “we don’t care how much of positive contribution you are to society, we don’t want you and we want to treat you like you are second class citizens.”  People run around and say “we don’t want to give ‘special rights’ to the gays.”  All of this is thinly veiled rhetoric that says “we don’t like gays and we are going to stick our noses in your lives to make ourselves feel better.”  I would much rather have people say what they mean, like Pastor Sean Harris, because people can easily identify the biggots for what they are.

So President Obama, I am glad your thoughts have evolved, but until there is legislation on the books that allows immigration based on our relationship at a federal level, your views have not evolved enough.

Just git on with it…

Ok, coding nerd moment here.  I have been becoming more and more involved in my spare time with the Dojo Toolkit.  For me it keeps my mind in an enjoyable “problem solving” mode, which I enjoy, versus necessarily all the fun of being a senior IT manager affords itself, where I wouldn’t get near the sharp end of code even if I wanted to.

Because of that, I have started to investigate git and github.  I had been impressed with Subversion before, but git and github are a world of difference in my mind.  The whole ability to fork, push and pull makes it great for me to hang out on my own, but gives me the ability to lob in contributions in a way that it is easy to manage and support.  For Open Source development it is the only way to go.  Part of me didn’t want to investigate it, because all the “cool kids” were using it, which usually means I like something else that is slightly more interesting and cool.

Of course I told my partner about how interesting git was and that all the “cool kids” were using it, to which he replied “You mean the nerdy cool kids?  Think about what you are saying.”  Oh well…  Back off to nerd land, sometimes far better than reality…

2012

I have tried to make New Year’s resolutions before, but they usually come to naught.  So this isn’t a news years resolution post, this is just a plan.

Simon and I have been together for 12 years now and have been in a Civil Partnership for 5 years.  We have plans this year to start the rest of our lives.  While we have often tried to make progress on whatever you might call long term plans, they are always slow to come.  This year though we have lots to make concrete movements on.  If we make the right progress then 2013 will be a year of big change for both of us and 2012 will be the year that made it possible.

What happens when you have two IT professionals start planning your life?  You come up with a Microsoft Project Plan, assign and schedule your tasks and have a weekly review meeting.  It is sad but true.  On the other hand, for Simon who is always making lists and for me who avoids lists unless I am totally under stress, it has helped us create a common place for us to focus our personal lives.  Of course day to day life always interferes with these plans, but at least they don’t get lost in the ether.  We have them written down and we know where we are going as a couple.

Also, in about 30 days, I will be celebrating the one year anniversary of my weight loss surgery.  I can hardly remember the man that was 100lbs heavier.  Most people who know me can’t remember him either.  My weight has stabilised and I have been +/- 3lbs of 195 for 3 months or so now.  If you average out my weight, I might have lost 1-2 lbs over that period, so it is still a slight downward trend.  Now that I know that my weight is stabilised, getting plastic surgery becomes a serious consideration.  I really enjoy the freedom I have, in that for the most part, thinking about food has become a minor factor in my life.

Also, Simon and I get to spend some time at the Summer Olympics in London.  It will make for a once in a lifetime experience I am sure.  We get to attend 8 events, with Beach Volleyball on the Royal Mall probably being the most interesting venue I can think of.

My sister will be giving birth to Tristan, my first “proper” nephew (or niece for that matter) right around Simon’s birthday.  We also found out over Christmas that Simon’s sister is also going to be giving birth (her second child) around June near my birthday.  So lots of exciting family events coming up this year.  This only makes Simon and I think more and more about family ourselves, which we plan to put significant wheels in motion this year in order to achieve that.

Two Idiots Abroad

My boss and I recently went on trip to India.  We called ourselves “Two Idiots Abroad” which was apropos we felt since we worked for Sky and Sky had the TV Show An Idiot Abroad.  For any of those who have seen the series, us, in India, wasn’t far off.  An American and a Scotsman dumped in the middle of India, visiting 5 vendor locations in 4 cities in 5 days.

We arrived on Monday morning at about 1AM and we got up at 4AM on Saturday to make our flights.  As any modern large corporation these days, we outsource some of our IT operations to India.  We try to give senior management presence in India a few times a year (I guess I am considered almost senior management these days).

In 1998 I had been to Hong Kong, which turned my senses and view of the world upside down.  In 2008 I went to South Africa and saw a clear distinction between the haves and have-nots.  So being sort of shoved outside of my comfort zone and having my senses assaulted by a different world I was prepared for, though I didn’t know quite what to expect.  My boss, who had been twice before, was quite curious to see how I would react.  I don’t know what he expected, like my head would explode or something, but I don’t think it did.

Being sort of encased in an expatriate business travel world, it is hard to get a full appreciation of what things are really like, but you can try.  Like the time I spent in Hong Kong, I very much identified with that world which was very well portrayed in Lost in Translation.  Where you get treated very much like some sort of alien race to be placated unless the incur the fury of the foreign invaders.  You also find yourself generally sucked into a social environment out of necessity instead of out of choice.  Luckily my boss and I were able to keep to ourselves, but I am sure if I was alone, I might have been even more sucked into that world.

The best thing though that happened to us was that we had an offer from one of the vendors to play some golf before we left the next morning.  While my boss (as well as I) are not fond of “being entertained”, my boss, being a keen golfer and I, well let’s just say I was quite interested in the cultural experience and being able to say I played golf in India, we decided it was a good idea, knowing it would come at the end of a hard week.

That experience will go down, I think, in both our books as one of those really memorable experiences we will recount for the rest of our lives.  Once of the management team from India had grown up in Bangalore and had some school friends that he liked to golf with.  So it was a very casual atmosphere, with business totally left behind.  Not only was the golf enjoyable, but the company was the best part of it.  We broke out of that cocoon of expatriatism.  We had some food at the club house afterwards, had a few drinks and just chatted about life and how things are similar and different being separated by 6000 miles.  Truly enjoyable!

The thing that I most noticed about India was the excess of people.  I kept calling it the “Division of Labour” which I think is the potentially “scary” thing about for us in the west, if we were worried about world dominance.  Having excess cheap labour allows specialisation which we haven’t had in the west for a long long time.  While I knew it was going to happen, largely based on the excellent book The White Tiger by Aravind Adiga, that if we had a driver, he would be our driver for the day.  What I didn’t realise was that he was our driver from the time we set foot in the city until we left again.  He would pick us up and drop us off at all unreasonable hours of the day, always nearby if we needed him.  My western mind kept thinking “what does he do all day”.

Almost all the conference rooms we were shuttled into had a dedicated attendant who would fetch us coffee or tea or anything at our beaconed call.  A couple of times we needed a level of support in setting up a projector or using something in the conference room and I think in every case at least two people showed up to try to resolve the issue.  I was walked to every room in every hotel I checked into and often chatted to make me feel welcome.  You simply get used to people schlepping your bags around without you even being consciously aware of it after a while.  With such a division of labour, India can simply people away any problem.

The biggest challenge is directing them in the right way.  The number of half done construction projects littered across the cities of India was staggering.  Discussion with locals often resulted in eye rolling about how the state couldn’t get agreement with the national government and all sorts of rows and debate about why the whole project was stalled.  I am sure west during the Industrial Revolution was in a similar situation, so much power and might, just not all of it focused in the right direction.  It is easy to see though why the west may very well be a dying beast to the likes of India and China now.

One of the things my boss and I wanted to do was to keep the troops back home entertained, and one of the ways we did that was that I decided to shoot some video and create a summary of our experiences, which I make available to you below for your viewing pleasure:

[youtube_video]http://www.youtube.com/watch?v=7wy1LI4KxOE[/youtube_video]