Knockout.js is great! I love the automatic UI refreshing that it does on the items that it has data bound.  Not having to manually hookup these events saves a lot of time.

In the example below, I have a couple textboxes, a checkbox that initiates hiding or showing of the output, and there is also a button that removes the first name.

All of these are hooked up with this code pretty easily.

Check out the fiddle below: 

 

 

This is great. The UI refreshes automatically!

The problem is that if you look at the data-bind attribute, you can see that that these are all magic strings that end up being the binding.  If you have to change the model, there is no compile time checking that flags these as needing to be changed.  You then have to do a find and replace and hope that you didn't inadvertently change text that was named similar.

What if I have to change the "lastName" in the model to be "surname" instead. 

Yuck!

Searching around to see if there were better ways accomplish this and I was just unaware, I found a blog that has helpers to use with knockout to get that compile time checking.

It's called: Fluent Knockout Helpers.  

I downloaded the helper from GitHub and installed it into my application.

To get this to work was pretty easy.

First, I set up my model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace PlayWithKnockout.Models
{
    public class Person
    {
        public string firstName { get; set; }
        public string lastName { get; set; }
        public bool removeFirstName { get; set; }
        public bool nameVisible { get; set; }
    }
}

 Then I created my view:

@using FluentKnockoutHelpers.Core
@using PlayWithKnockout.Models
@model PlayWithKnockout.Models.Person

<script src="~/Scripts/knockout-2.2.1.js"></script>
<script src="~/Scripts/jquery-1.7.1.min.js"></script>

<script type="text/javascript">
    $(function () {
        var viewModel = {
            firstName: ko.observable("Peter"),
            lastName: ko.observable("Parker"),
            removeFirstName: function () {
                this.firstName("");
            },
            nameVisible: ko.observable(true)
        };

        ko.applyBindings(viewModel);

    });

</script>

Old version with magic strings!
<br />
First Name: <input type="text" data-bind="value: firstName, valueUpdate: 'afterkeydown'" />
Last Name: <input type="text" data-bind="value: lastName, valueUpdate: 'afterkeydown'" />
<input type="checkbox" data-bind="checked: nameVisible" />
<p>
    Hello, <span data-bind="text: firstName, visible: nameVisible"></span>
    <span data-bind="text: lastName, visible: nameVisible"></span>
</p>
<button data-bind="click: removeFirstName">Remove first Name</button>
<br />
<br />

New version with compile time checking!
<br />
@{
    var fkoPersonHelper = this.KnockoutHelperForType<Person>();
}

First Name: @fkoPersonHelper.BoundTextBoxFor(mm=>mm.firstName)
Last Name: @fkoPersonHelper.BoundTextBoxFor(mm=>mm.lastName)

<input type="checkbox" @fkoPersonHelper.DataBind(db=>db.Checked(person=>person.nameVisible)) />
<p>
    Hello, <span @fkoPersonHelper.DataBind(db=>db.Text(person=>person.firstName).Visible(person=>person.nameVisible))></span>
    <span @fkoPersonHelper.DataBind(db=>db.Text(person=>person.lastName).Visible(person=>person.nameVisible))></span>
</p>

<button @fkoPersonHelper.DataBind(db => db.Click(person => person.removeFirstName))>Remove first Name</button>

 

When I run the project it produces the view below:

 

This creates some nice compile time checking. 

So now I can change the model from lastName to surname and it will throw an error for me.

Next up is to see if I can set the observables using the helper to reduce the javascript code.  Stay tuned.

 

If you want more info on the helper itself, check out John's blog here...

 


I downloaded and installed Visual Studio the other day.  I had heard about all the controversy on the color scheme and was a least prepared for it, but wow visually, there are a lot of changes.

 

Take a look at the top section.

 

The first thing I notice is the menu names are all in caps.  Can someone tell Visual Studio to stop yelling at me?  Caps to me just screams yelling.  There is also lots of grey space and no separation between most icons. 

Speaking of icons it looks like Microsoft decided to change every single icon in the VS - well except for the save icon.  That hasn't changed in 40 years.

 

 

 

Not all of it is bad though.  It's the feature set right?

One nice new addition is the ability to have it run in the browser of your choosing.  You can now use Firefox out of the gate as your browser of choice.

 

 

Another nice addition is the File Preview mode (in the image below it is the icon to the right of the wrench).  This mode enables one click file views that places the tab on the right side instead of the normal left side for files open.  This will help cut down the clicks.  It only stays open until you click another file.  If you want it to remain as an open file, then you need to double click the file in solution explorer.

 

 

Well that's it for now.  I'm sure the more I use Visual Studio 2012 the more I will get used to the new look. 

Have you adapted to the new look and feel? 

 



 Hello and welcome to my blog.   My name is Chris Holwerda and I am currently a service governance lead developer who is building a service oriented architecture for a financial company based in Minnesota. 

I wrote my first program using a TI-81 graphical calculator back in 1992.  It wasn’t terribly fancy, but it was really cool to come up with a formula and program to solve all those “if a train left Chicago heading to Minneapolis was traveling at 90 miles an hour and another train was leaving from Minneapolis heading to Chicago at 35 miles an hour , with a distance between Minneapolis and Chicago of 409 miles, How many minutes into the journey would they crash.”    From that point on, I was hooked into creating software applications to perform everyday functions.  

I enrolled in getting a programming certificate and for a couple years learned about assembly, cobol, fortran, c++, and visual basic 6.  Yes, VB6 folks.  What I learned out of this is that I didn’t want to write in assembly language!  I was lucky enough to have the opportunity during school to write software for a local eyeglass company by writing the order system in VB6 and integrating with a UNIX system to complete the order process. 

I moved on from the eyeglass company to a 3rd party trucking logistics company and into their credit card and financial division.  I’ve been there ever since which has been about 13 years.

Through the numerous years, the many technical challenges, and the language and design changes, I can say that a developer’s life is never dull or boring.  There is always the need to learn new things.  Whether it be VB6, Classic ASP, HTML, VB.NET, ASP.NET,  XML, Javascript, C#, ASP.NET MVC, Java, T4,  JQuery or the many others;  there is always the next greatest thing right around the corner to learn about.  Development is all about temporary knowledge has been my favorite mantra.

My main focus is the Microsoft stack although I love to dabble in other languages so you may see posts about a wide range of things.  I hope to also share some personal experiences or other random thoughts that I feel are engaging from some of my favorite hobbies like backpacking, scuba diving, and photography. Thanks for reading and I hope to hear from you. 

 

The answer is 196 minutes into the journey, the trains will crash.

 

409miles = ((90mph * t) + (35mph* t)) / (60min/hour)

24540 = 90t + 35t

24540= 125t

24540/125 = t

196 = t

 


Chris Holwerda

Development musings and adventures