Random Thoughts

Some random thoughts for now. I am going to try and start posting regularly. I have added a calendar reminder to make sure I don’t get busy and forget.

I recently opened a Bitbucket account. I chose Bitbucket over Github because Bitbucket will give you private repos as part of the free plan. I have started integrating it with my daily workflow and it is easier and less intrusive than I thought. I also started using Source Tree, which is a free Git client from Atlassian. That may be why using Bitbucket has been so easy. Atlassian also has a set of Git tutorials that helped with the learning process.

I am still using Knockout.js for some work projects. I enjoy using it. The 2-way data binding really makes things simple to work with. One update that I made in 15 minutes would have taken about 2-3 hours if everything was coded by hand. The documentation has been helpful and anything I haven’t found in the documentation I have been able to figure out with help from Stack Overflow.

Preventing backspace from navigating your web page

I was running into problems where a user would have clicked outside of an input box and then type a backspace key and have the webpage navigate back one page. I tried a couple of solutions of my own, but they weren’t working.

I found this Stackoverflow solution which fixed the problem.

// Prevent the backspace key from navigating back.
$(document).unbind('keydown').bind('keydown', function (event) {
var doPrevent = false;
if (event.keyCode === 8) {
var d = event.srcElement || event.target;
if ((d.tagName.toUpperCase() === 'INPUT' &&
d.type.toUpperCase() === 'TEXT' ||
d.type.toUpperCase() === 'PASSWORD' ||
d.type.toUpperCase() === 'FILE' ||
d.type.toUpperCase() === 'EMAIL' ||
d.type.toUpperCase() === 'SEARCH' ||
d.type.toUpperCase() === 'DATE' )
) ||
d.tagName.toUpperCase() === 'TEXTAREA') {
doPrevent = d.readOnly || d.disabled;
else {
doPrevent = true;

if (doPrevent) {

Knockout Auto-complete

I have been using knockout.js extensively in a recent project. I have enjoyed using it. The two-way data-binding has really simplified syncing the display and data, as well as posting data back to the server. I have found the documentation to be superb. Anything that I didn’t find in the documentation, I was able to learn with a quick google and usually found the result I needed on stackoverflow.com.

One feature that wasn’t explained well in the documentation was linking knockout to jQueryUI’s Auto-complete. I found several other blog posts from other people that had implemented it, but I think this post provided the best implementation. There were a couple of items that I tweaked to make it work closer to the way I wanted.

The first change was in the knockout custom binding. On the input text box, I had also defined a change event binding. The event binding was not getting called. There is a function named updateElementValueWithLabel in the definition that is called whenever an item was selected, changed or focused. I added this code to the bottom of the function:

if(typeof allBindings.event.change !== "undefined") {

This code enabled the change event binding to be called. It did create another problem. The above function was also being called when on option was moused over which triggered a focus event in the autocomplete plugin. I didn’t need the change event fired on just receiving focus, so this led to the second change I made.

In the jQueryUI section of the code, I changed the focus option to run just one line of code instead of calling the updateElementValueWithLabel function. That code was:
That line placed the option value in the html text box. When someone tabbed out or hit the enter key, the select/change functions would then be called.

I appreciate all the work the original author put in to develop this code. It saved me a tremendous amount of time in figuring out how to implement this and I only needed to make some minor tweaks to get it to work the way I needed it. Thanks Cameron.

PHP JSON POST data quirk

I encountered a strange PHP post data quirk. I was posting data to a PHP script using a jQuery AJAX call. I was posting all the data as a JSON string. When the PHP script was invoked the $_POST variable was totally empty. I also check $_REQUEST and $_GET and there was no data. I checked the headers and could see the data being passed, but it was not showing up in the standard PHP pre-defined variables. I did some googling and finally came up with this command:

$incoming = json_decode(file_get_contents("php://input"));

That ended up as an object with all of the data passed.


I have recently started using knockoutjs.com. I have been looking at other frameworks for a while, but I finally settled on using knockout. I like the clean code you can use. I never liked having to start your objects/classes by extending a framework base class. There is some KO specific code that you have to include, but it doesn’t take over your code.

I like Durandal as well, but it is in the process of being phased out. The creator of that framework is now on the Angular 2.0 team and will be integrating features of Durandal into Angular. It is unclear when Angular 2.0 will be released and I didn’t want to start using a framework knowing an eventual migration was in the works.

Stay tuned for more Knockout related posts.

New Laptop

The Toshiba laptop I had for over 8 years finally died. I replaced it with Gigabyte P27K-CF2 17.3-Inch Laptop. This is advertised as a gaming laptop, but it makes a great laptop for developers as well. It has lots of memory for running multiple virtual machines at the same time and has lots of expansion as well. The screen is a HD screen and movies look great on it. Another nice thing is the laptop didn’t come loaded with bloatware, so I didn’t have to waste time uninstalling a lot of stuff.

JavaScript framework

I have been reviewing JavaScript frameworks. I have not been satisfied with the popular frameworks. I recently started looking at Durandal. It is based on Require.js so it is easy to create modules to organize your code. From what I have read, you do not have to start from a base durandal object. You just work with POJO (plain old javascript objects). That way you can also use your modules in non-durandal code as well. This is definitely a framework I am going to spend more time investigating.

Neat JavaScript trick

I have been reading Secrets of the JavaScript Ninja and learned an new idea. John Resig showed how to cache DOM elements by creating a cache inside of a function. He refers to this as a self-memoizing function and there is an example on page 75. Here is his example:

function getElements(name) {
  if (!getElements.cache) getElements.cache = {};
  return getElements.cache[name] = getElements.cache[name] || 

I can  already think of where I can use this to improve performance on a website.