Wednesday, December 29, 2010

Binding Json to Forms by Convention

I'm sure there are lots of javaScript frameworks out there that will let you do the same thing, but here is some handy code to bind data from a json array to HTML elements by naming CSS classes after the JSON data elements. For instance, a JSON element like this:

{"Name":"Chris"}

Could be set up to be bound to an HTML element like this:

<input type="text" class="Name"></input>

Simply by using a CSS class name that matches the JSON field name, the data gets bound. This code works for any form input type, and non form elements (label, span, div). I have also provided a function to clear a form. Below is a fully functioning example that you save as html and play with.

<html>
 <head>
  <script src="https://www.google.com/jsapi"></script> 
  <script> 
   google.load('jquery', '1.4.4'); 
  </script> 
  <script type="text/javascript"> 
   function BindJsonToDialog(fielddata, formId, cssClassPrefix) {
       var context = document.getElementById(formId);
       var selectorPrefix = " ." + cssClassPrefix;
       for (key in fielddata) {
           var ctl = $(selectorPrefix + key, context);
           if (ctl.length != 0) {
             var isTextField = (ctl[0].tagName == "DIV" || ctl[0].tagName == "SPAN" || ctl[0].tagName == "LABEL");
             if (isTextField)
              ctl.text(fielddata[key]);
             if (ctl[0].type == 'checkbox')
              ctl.attr('checked', (fielddata[key] == true));
             else
                 ctl.val(fielddata[key]);
         }
       }
   }
       
   function ClearDataFields(formId, clearClass) {
       $("#" + formId + " :input").each(function(){
           var type = this.type;
           var tag= this.tagName.toLowerCase();
           if (type == 'text' || type == 'password' || tag == 'textarea' || type == 'hidden')
               this.value = "";
           else if (type == 'checkbox' || type == 'radio')
               this.checked = false;
           else if (tag == 'select')
               this.selectedIndex = 0;
       })
       //clearClass needed to clear non-input elements
       $("." + clearClass).text("");
   }
   
   //Sample data
   var jsonString = "{'Name':'Chris Clark','City':'Charleston','Check1':true,'Check2':false,'Email':'chris at untrod dot com'}"
   var jsonData = eval("(" + jsonString + ")");
   
  </script>
 </head>
 <body>
  <div id="myForm">
   <div>Name: <span class="dfName clearMe"></span></div>
   <div>Email: <input type="text" class="dfEmail"></input></div>
   <div>Checked: <input type="checkbox" class="dfCheck1"></input></div>
   <div>Unchecked: <input type="checkbox" class="dfCheck2"></input></div>
   <div>City: <input type="text" class="dfCity"></input></div>
  </div>
  <div style="padding-top:10px; cursor:pointer;" onClick="BindJsonToDialog(jsonData, 'myForm', 'df');">
   Click here to fill form
  </div>
  <div style="padding-top:10px; cursor:pointer;" onClick="ClearDataFields('myForm', 'clearMe');">
   Click here to clear form
  </div>
 </body>
</html>

Friday, December 3, 2010

Internet Radio and Mass Customization

I keep coming back to an article from the New Yorker: You, the DJ

It discusses the sociological effects of internet radio, primarily that if radio stations are tailored for specific listeners (ie. fredwilson.fm), two things happen:

1) The listener is not necessarily exposed to a wide range of music. With flesh-and-blood DJs, the listener is at the mercy of that DJ and may hear music that would have never been considered when building a tailored station. You don't know what you don't know.

2) The death of cultural commonality. It's far more difficult for music to "define a generation" or "take the world by storm" when the listener base is so fragmented.

The first point has a clear parallel with online news and article aggregators. For years there has been discussion about how, if readers can control the news that is delivered to them, they won't be exposed to points of view they might find disagreeable.

Broadly, if the future is mass-customization, then will we still have anything in common?

Wednesday, December 1, 2010

The Twitter Network Asymptote

Twitter users tend to add followers on a somewhat regular basis and, correspondingly, regular twitter users all see their follower counts gradually increasing. So the connectedness of Twitter is growing. I suspect, based on absolutely no data whatsoever, that if your normalize the number of connections on Twitter against the size of the userbase, and plot it against time, it looks something like this:


Twitter is still hitting its stride in terms of growth, so I suspect the growth of the connectedness is fairly linear. Or perhaps not it's even steeper with Twitter's introduction of features like "Who to Follow" and "Similar to...".

But at some point it has to stop, or else we find ourselves in a world where everyone is following everyone else, which doesn't make any sense.

Right now, un-following is not a common practice, but that line will start to taper, and eventually approach a horizontal asymptote - the steady state of Twitter connections. How will that happen? Will un-following become a common and more used? Or will people simply stop following each other?