Tagjavascript

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

H

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: it('throws error if myParam is less than 10', function(done) { var fn = function(){ myModule.doSomething(8, function(err) { if (err) throw err }); } assert.throws( function() { fn() }, /Value is less than 10/ ) done(); }); Assuming our module looks something like...

Using custom options with KnockoutJS Drag and Drop

U

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...

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

S

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...

Equivalent of LINQ projection / select with Javascript

E

Not strictly an ‘equivilent’ but a way of achieving the same. For example, consider the following array: var products = [{ id: 1, name: "Product One"}, { id: 2, name: "Product Two"}, { id: 5, name: "Another Product"} ]; Say, for example, we just wanted a list of IDs from this array. With .net, using LINQ, we could do a simple Lambda: .Select(x=> x.id) With Javascript, we can use Map to achieve...