Skip to main content

7 posts tagged with "javascript"

View All Tags

A little lesson on binding

· 2 min read

I've been writing JavaScript for the past 10 years for my work, and there are still things in the language that trips me up. A very good example is on binding.

Given the setup below, I would have been a little hesitant as to what the right answer is.

Backbone Events

· 6 min read

Backbone events are a powerful way to write responsive user interfaces with fewer lines of codes. This article attempts to give a detailed breakdown of the events and the parameters available for the callback functions.

The results in this article are applicable for version 1.2.3 of Backbone.js

The test is performed by instantiating instances of ExperimentModel extended from Backbone.Model and ExperimentCollection extended from Backbone.Collection.

The model is first added to the collection to test "add" on the collection and then a properrty is set on the model.

var expModelA = new ExperimentModel({ id: 'a' });
var expCollA = new ExperimentCollection();

Back to Basics

· 3 min read

When I started my formal education in computer science, I had to take a module on programming with the C language. I remember this was a module that caused many of my peers to re-think their decision to study computer engineering; the concept of pointers was so foreign to many of them that even the smart ones scored poorly.

Download JSON as a Text File

· 3 min read

As a Web developer, I sometimes find a need to download some huge JSON object into a text file.

Modern browsers now come with some form of developer tool/console to help debug the monstrous amount of JavaScript in the Web page. For Chrome and Firefox, I simply press F12 to bring up the console. From the console, you can naturally copy the JSON object in its string form by first converting the object into a string like so:

JSON.stringify(obj);

Then highlight the output from the developer console and press Ctrl-C to copy. The trouble comes in when the object is huge - to the tune of thousands of properties.

When an object gets to that size, you will need to scroll to be able to select the complete output. Scrolling the console is itself a tiresome task with text that small. Moreover you run the risk of "over scrolling" such that you select two objects rather than one because you can't tell the difference when they are simply chunks of text.

The best solution is to download the JSON as a text file and then use/manipulate the JSON from the file.

The way to do this is to create a function like this:

Solving the Mystery Behind Alternating Failed Tests in QUnit

· 3 min read

I’ve recently started using QUnit for my JavaScript testing. It’s a unit testing framework for JavaScript. It’s quite an easy framework to learn and I strongly encourage everyone who is doing any decent JavaScript coding to use it or any one of the myriad frameworks out there.

One of the strange problems I faced with QUnit is a strange phenomenon where a custom written plugin fails the test on every alternate invocations of the test.

JSON Object With Trailing Comma

· 2 min read

As any respectable JavaScript developer will know, JSON has become the de facto method of holding objects and passing arguments, even using it as a name-spacing mechanism.

Being the conscientious (and careless) programmer that I am, in my e-learning application that I’m developing at the moment, where I’m heavily using JSON, I made a conscious choice to always leave a comma at the end of the last member-value pair like so: