Blog posts tagged 'javascript'

How to assert.throws on an async function with a callback

Apr 1 14

Recently, I was writing a module that took a callback, and needed to write a test, asserting on an exception.

Here’s how to do it:

Assuming our module looks something like this:

Using custom options with KnockoutJS Drag and Drop

Apr 4 13

I was recently doing some work involving Knockout and needed to implement drag / drop (with jQuery UI draggable and droppable)

I stumbled across this post from Wilsonhut that shows how to use custom binding to attach drag and drop functionality to your view model.

Since that post (and previous posts in the series) covers that in detail, I won’t elaborate.
However, I had a requirement to be able to use some of my own options, for example appendTo.

To do this, I slightly modified the handler as follows:

This now allows me to do the following, with it still working as before:

Syntax error using Yahoo YUI Compressor .net port – works using java version

Jan 15 13

Recently, I was tasked with creating a minified version of a web applications Javascript files.

I settled on the Yahoo YUI Compressor, in particular, the .net port of it

When I tried to compress a particular file (the jquery.caret plugin), it would throw up a syntax error.

This error only occurred using the .net port of Yahoo YUI Compressor.
Using the standard java jar version of YUI Compressor did not throw such an error.

The error was a syntax error on this line:

It was saying invalid character at position 0.

After some time scratching my head, I opened the file in notepad++ and converted text to hex 16
Immediatlely, I could see there were some invalid characters around the function text:

Hex characters in jquery.caret

Removing those characters and saving the file solved my problem, and allowed the file to compress.

I looked at the issues page for the jquery.caret project, and found this issue had already been reported


I’ve created an issue on the codeplex page for this now:

Equivalent of LINQ projection / select with Javascript

Nov 27 12

Not strictly an ‘equivilent’ but a way of achieving the same.

For example, consider the following array:

Say, for example, we just wanted a list of IDs from this array.

With .net, using LINQ, we could do a simple Lambda:

With Javascript, we can use Map to achieve something similar:

I made this available as a jsFiddle for demonstration purposes: