I have spent the past 5 years predominantly within the Xaml arena and whilst I have appreciated the new skills I have learnt in terms of the specific technology, one of the greatest benefits to me has been the coupled approach towards design patterns synonymous with xaml. I’m talking about MVVM, composite applications, and how these, in turn, introduced me to the concepts of IOC, Repository and Unit Of work patterns. Whilst I realise these have been around for a lot longer than Xaml, the prevalence of them in the asp.net web forms arena was non-existent, at least for me at the time.
Fast forward 5 years and with MVC, I am fast rekindling my love of asp.net. Not only is the base framework a significant improvement on web forms, but the explosion of complementary JS frameworks from JQuery to Knockout have really enriched the choice for web developers to pick the right tools for the job at hand.
Recently, I have been looking at Knockout and Web Api with view to minimising the number of repeated page or partial page refreshes when updating data. Using JQuery Ajax to grab data is a great way to remove the overhead of returning page details on top of the actual data itself. Coupled with knockoutjs to provide a intuitive workflow is a natural choice but my first pass demonstrated immediately the need for some formatting:
These came about from the following Knockout syntax:
In xaml, where databinding is (relatively) automatic, a simple StringFormat would provide the option here to mould the date required but using knockoutjs, one way I found to manipulate data bindings is using the custom bindings feature coupled with a suitable 3rd party framework if required. Moment.js is an excellent library for manipulating dates. Using the custom binding with knockout and providing the option to pass in a custom format string, I came up with the following:
This will look for the formatString parameter and will default to the english format of dd/mm/yy if it is not present. In my markup, I now have the following:
Which gives me the following output: