Wednesday, March 14, 2012

Russian Ark - *****

OK, I was a bit late to see this movie. Well, by almost 10 years. But that is what I like: discovering when the hype is completely gone.

If you do not know anything about it, here is what you would commonly hear about it: "The whole film is just one shot, and it spans the whole 96 minutes of the film".

Second is that in the course of the film, you would see stunning pictures, I am saying stunning - you got to see it.

But, but.

This is not really what the film is about. For starter, Russian Ark is not the first single-shot movie (first being Hitchcock's). It is not even the second one. So no novelty here. There was a huge logistics to make this happen but yet again that is not what makes a film a memorable one.

Also, I have seen films with stunning pictures and I did not like them. Movies of the late Theo Angelopoulos were among them - he may rest in peace but I would not withhold my view. Weeping Meadow was weak, really weak - and I cannot understand why the Cannes prize. Anyhow - enough talking behind the passed away.

The film is about history. Well, that was obvious, wasn't? Covering three centuries of Russian Monarchy history in an hour and half with location being Hermitage museum. And showing so many historical artefacts, painting, people, costumes... Yet this obviousness transpires towards the end of the film into something greater. It is a celebration of us mortal beings and our vague yet eternal legacy. Story so many dead people narrated by the spirits of two dead strangers - yet it is a celebration of life.

Finale is worth watching the whole thing if you get bored halfway. Stick with it and you will be graciously awarded for your patience.

This is 5 stars and immediately among my top 100 movies of all time.

Thursday, February 02, 2012

Introducing conditional.js, a jquery plugin for clean predicate-action separation

These days javascript space is full of newly developed and emerging frameworks. It is still early to speculate which ones will make it and can become par to jquery's unique status, but some have already made it into quite a few production projects and are gaining momentum and popularity by the day.

Depending on your background - be it Open Source Java/Ruby/Python or Microsoft's .NET - examples could be different but one is the MVVM Framework for javascript - aka knockout. I have been and am a fan of all the possibilities this framework brings to the client-side code but sometimes all you need is a simple tool to organise your code and make it more readable - you do not need a full-fledged MVVM library.

Let's bring an example.

Consider we have a form where depending on the answer to one question, answering some other questions become mandatory. We have a form asking for our detail and if the answer to marital status from a dropdown is "married", then detail of the spouse needs to be provided.

If we were to build this in HTML/javascript, we would put the fields for spouse detail in a container and then hide/show based on the value selected from the dropdown.

Here we have a predicate: "whether marital status of married is selected" and we have an action which is "show or hide a container". Here the action is parameterised with a boolean which happens to be the return value of the predicate.


Coding this is almost trivial: use jquery (or plain old DOM events) to subscribe to changes of the dropdown value and hide/show accordingly. And none of that predicate/action nonsense!


But as the requirements gets more complex, the code gets more and more complex to the point where the intention of the code is buried underneath many lines of code. For example, what if the logic depends on the value of multiple fields? The complexity will increase exponentially with the number of fields involved. Also one common problems - those of you who have implemented such scenario would immediately remember - is that when the value is served by the server, the event is not triggered so the visibility of the container is not affected. For example, if we edit a saved form for a married person, server will send the HTML with the value of married status dropdown to "married", however, since the change event is not triggered, spouse detail container stays hidden. To combat this, we have to raise the change (or call the callback code) so that the logic is executed.

One solution is to use a framework such as knockout to wire the event with the logic as a client-side binding. One problem with such approach is that the binding to multiple fields is cumbersome, especially if they are involved in more than one such logic. Also, implementing custom action requires adding custom binding which is fine with one or two cases but not great when there are more custom action to be executed.

The solution conditional.js presents here is to a separation of predicate and action by:

  1. Subscribe to change events of one or multiple elements by providing one or more jquery selectors
  2. Accepting a predicate (a javascript callback function) that will receive the value of the elements
  3. A set of standard actions (hide/show and enable/disable) that will react to the value returned by the predicate. Alternatively registering a custom action, or simply just providing a callback action that will be called passing the return value of the predicate.

This probably sounds more complex than it is so let's bring the example - example above but this time with code.


        $("#spouseContainer").conditional({
            selectors: "#marst",
            predicate: function (maritalStatus) { return maritalStatus==="Married"; }
        });

As can be seen above, container with id of "spouseContainer" is used to start create the jquery object. We pass selectors (here a single string and it can be a string array too) that is for the marital status dropdown with id of "spouseContainer" and then a predicate defining our desirable condition which by default is visibility - i.e. whenever value of "marst" is equal to "Married", spouseContainer will be visible. Since we have passed a single selector, our predicate needs to accept a single parameter.

So I believe this 3 lines of code are really expressive and readable. This also take care of the problem of setting the status when the page is loaded with the desirable value.

In the next blog, I will delve into all options and possibilities of conditional.js.