JavaScript

Javascript: Alias a function and call it with the correct scope

Today I needed to call one of two possible functions depending on a condition. The interface into these functions was the same (they accepted the same parameters) and they returned data in the same format.

getData() {
  const params = {
    orgId: this.orgId,
    siteId: this.siteId
  };

  this.dataService.getFullData(params)
     .then(result => {
        // ... do stuff
      })
      .catch(this.handleError);
}

Essentially what I want to do is call a leaner “getLeanData” function instead of “getFullData”, if no siteId is provided.

There are few approaches to this problem. One would be to change the way getFullData worked, and to move any switching logic into it. The other would be to break up the promise callback functionality and move it into a separate function, and to just have an if block.

I didn’t really like either of those approaches and knew that I could alias the function that I wanted to call instead. Here was my first, non working attempt:

getData() {
  const params = {
    orgId: this.orgId,
    siteId: this.siteId
  };

  let dataFunction = this.dataService.getFullData;

  if (typeof params.siteId === 'undefined') {
    dataFunction = this.dataService.getLeanData;
  }

  dataFunction(params)
     .then(result => {
        // ... do stuff
      })
      .catch(this.handleError);
}

The above looked like the right approach, and would have worked if:

  • This was not an ES6 class. Operating in strict mode means that the scope of the function being called (this) is actually something, and not just the global scope.
  • The data functions were local to the ES6 class, and not in a depended upon class

Basically, the function call worked, but it was not executing the dataFunction within the scope of the dataService class – resulting in errors.

Why is this happening?

This happens because when assigning a function to the local variable “dataFunction”, only the body of the function, not the object that it needs to be called on, is copied. If the getFullData and getLeanData functions contained non scope specific code, such as a simple console log statement, the behaviour would have been as expected. However in this case, we need the scope of the dataFunction class.

The solution

The solution is to call the function with the scope (officially called the “thisArg”) explicitly set. This can be done using the Function​.prototype​.call() method.

getData() {
  const params = {
    orgId: this.orgId,
    siteId: this.siteId
  };

  let dataFunction = this.dataService.getFullData;

  if (typeof params.siteId === 'undefined') {
    dataFunction = this.dataService.getLeanData;
  }

  dataFunction.call(this.dataService, params)
     .then(result => {
        // ... do stuff
      })
      .catch(this.handleError);
}

Calling .bind on the function call will also work with you, but I think the use of .call is a little more readable

hardware

Surface Book keyboard review

I’ve said it before, and I’ll say it again – I prefer chiclet keyboards. In my opinion, they are more suited to a long days worth of typing and programming than a mechanical keyboard, or a more conventional keyboard.

The Surface Book keyboard is the second chiclet keyboard that I’ve purchased in the last month, with the previous one being the Cherry KC 6000. I bought the Surface Book keyboard to be a replacement for my home setup, where I was previously using an ageing and horribly tatty Microsoft Ergonomic USB natural keyboard.

The requirements

My requirements for my home keyboard were slightly different to my workplace keyboard requirements:

  • Must have chiclet keys
  • Must not look too garish. Whilst this is for my home setup, I like having a neat desk, and a luminous keyboard with fancy lighting just wouldn’t fit into this
  • Must not be a natural keyboard. I had previously used an ergonomic natural keyboard for a good few years. It was good in the sense that I didn’t get any discomfort using it, but I don’t think my typing style was every completely suited to it. My left hand in particular liked to travel into the right hand section of the keyboard during some keypress combos. I also found some aspects about the key layout to be jarring – such as the double spaced “n” key.
  • Must be bluetooth. Like I say, I keep a neat desk and the fewer wires, the better.
  • Must have a numeric keypad
  • Must not have any keys in strange places. As a developer, I use the ctrl, alt, super, home, end and paging keys a lot. Any re-arrangement of these keys would probably impact my productivity, so these must firmly stay where they would normally be

The matching keyboards

I found two keyboard that matched my needs. The first was the Logitech Craft, which also has the added bonuses of being able to pair with multiple devices, and also has a big wheel that can be used for additional control, although this sounds like it does not typically stretch beyond volume control:

However, there are two downsides to this keyboard:

1 – it’s expensive, priced at about £160. Whilst I think that as a developer I need the best tooling, this just feels like a stretch.

2 – It’s hard to get hold of. When I was trying to purchase this keyboard, I struggled to find anyone that actually had it in stock, including Amazon. I eventually found it in stock on very.co.uk, but it wasn’t really in stock and they sat on my order for over a week before I lost my patience and cancelled.

This left one other keyboard – the Surface Book Keyboard.

Surface Book Keyboard Pros

The keyboard is aesthetically very pleasing, with a small bezel and a simple and tasteful grey and white colour scheme used. The Bluetooth connection helps with the appearance of the keyboard as it means you don’t have any wires to try and hide or make neat. I’d describe the footprint of the keyboard as low profile, as it has such small bezels and looks discreet yet impressive in the middle of your desk in complete isolation from any sort of cabling.

It is super comfortable to type on, with the key presses feeling light yet satisfying to depress, with a sufficient level of feedback delivered to your finger tips. Typing on it is pleasurable and fast.

The general typing comfort is helped along by a healthy level of banking on the keyboard towards the user, which is something that the Cherry KC 6000 fails at. The brilliant thing about this banking is that it’s a stroke of smart design – the bank towards the user comes from the battery compartment:

The keyboard layout is sensible, and doesn’t try to be too clever in this area by re-stacking keys or jigging around the layout of anything. It very much is laid out like you would find a laptop keyboard – with the Function lock key placed between the Ctrl and Super keys. This probably is a big plus for you if you tend to dock your laptop and work off of it, or if you’re used to working on a laptop keyboard. Plus points for me on both – when I’m at home, I work off of my laptop on a stand.

Availability wise, this keyboard is very easy to get hold of. I ordered this online at about 4pm through PC World’s website, and was able to collect it the next day at 11am from my local store. It’s also well stocked elsewhere around the web, which is a marked departure from my experience when trying to purchase the Logitech Craft.

Cost wise, the Surface Book keyboard can be yours for £79.99. This appears to be a fixed price, much in the way that Apple price their hardware. It’s the same price everywhere, unless you look at used items. Here are some links:

Surface Book keyboard cons

The only real downside for me (and this is a nitpick) with the Surface Book keyboard is that it can only be paired to one device at a time. I often switch between my desktop PC for gaming, and my Xubuntu laptop for everything else. This means that every time I do this, I need to pair the keyboard again. Luckily, this isn’t much more than a slight inconvenience, as the pairing is a quick and painless experience on both Windows and Ubuntu based operating systems from version 18 onwards.

Protip – don’t throw away your USB keyboard

USB keyboards have two big advantages. They don’t need batteries, and they will work as soon as they have a physical connection, even whilst your machine is still booting up. If you need to do anything in the BIOS, for example, you will not be able to do this with a Bluetooth keyboard as the drivers for it will not have been loaded. So, keep your dusty old USB keyboard for the day when you run out of power and have no batteries, or for when you need to jump into your BIOS.

Conclusion

On the whole, this keyboard is fantastic, and I’ve give it a 9 out of 10. I’d highly recommend this keyboard for general typing, programming, and some gaming.

hardware, Xubuntu

Does the Surface Keyboard work with Ubuntu? Yes, but only with Ubuntu 18 onwards

The other keyboard that I’ve recently purchased as well as the Cherry KC 6000 is the Microsoft Surface Keyboard. After reading a few reviews online and watching a few videos, I decided on getting this bluetooth keyboard for my home setup.

My main concern however, was whether this keyboard would work with my Xubuntu laptop. Some googling revealed mixed answers – someone running Linux Mint, which is based on Ubuntu, had no luck. Whereas a separate thread on Reddit seemed to indicate that it worked without any problems.

After purchasing the keyboard and using it, here is what I found: In order to successfully pair the keyboard with any PC, the keyboard will ask the PC to present the user with a passcode challenge. On Windows, when pairing, a dialogue will pop up that contains a 6 digit number. It will prompt you to enter a 6 digit passcode on the keyboard. One this is entered on the Surface Keyboard, it will be paired and will work as expected. When pairing the keyboard with my Xubuntu laptop, here’s what I found:

The Surface Keyboard will not work with Ubuntu 16 or any derivatives

This is because of a few bugs in the bluetooth stack in Ubuntu 16. The passcode dialogue will never appear, meaning that you will not be able to successfully pair the keyboard to a machine running Ubuntu 16 or below. This problem also happens when attempting to pair in the terminal using bluetoothctl.

The Surface Keyboard will work with Ubuntu 18 upwards

Luckily, I have a spare laptop, and was able to use that to test out the pairing on Ubuntu 18 before committing myself to upgrading my main work laptop. Using the spare, I was able to successfully pair on Ubuntu 18.04 through the bluetooth gui as the passcode prompt was now appearing. I took the plunge and updated my main Xubuntu laptop and can confirm that the pairing with the Surface Book keyboard fully works on Ubuntu 18.04 and any derivatives.

Xubuntu

How to fix: Xubuntu update manager not showing new distro releases

I’d previously been running Xubuntu 16.04 on my main laptop without any issues, and had been waiting for a while before upgrading to Xubuntu 18.04.

Because I was having problems with bluetooth, and some furious googling had lead me to conclude that a distribution upgrade would resolve my issues, I decided that now was a suitable time to update my operating system.

Xubuntu’s documentation states that when there is a major LTS release available for you to upgrade to, the update manager will pop up with a dialogue box informing you, and giving you the option to update. It looks similar to this:

You will not get this prompt if you have broken PPA repository URLs configured. The best way to find out which PPA URLs are causing problems, is to run…

$ sudo apt-get update

From your terminal, and observing the results:

Reading package lists... Done   
E: The repository 'http://ppa.launchpad.net/pinta-maintainers/pinta-stable/ubuntu xenial Release' does not have a Release file

In my case, my PPA configuration for the excellent image editor, Pinta, appeared to be broken. Simply disabling this PPA from the software updater by unchecking the checkbox allowed the OS to fully pull update information, and then prompted me with the distribution update dialogue.