Using browserify to manage JavaScript dependencies

Managing JavaScript dependencies in the browser is hard. Library scripts typically create global variables and functions. Other scripts now depend on those global objects to do their work. This works but in order to load all required scripts we have to add <script> elements to our HTML, making sure to add them in the right order, and basically know what each exposes. The problem Consider the following client side code: 1: // Print a message 2: utils.print(“Hello”);   This depends on another piece of script below: 1: // Expose the utility object with it’s print function 2: var utils = … Continue reading Using browserify to manage JavaScript dependencies

X things every JavaScript developer should know: Automatic Semicolon Insertion

As with many other things in JavaScript Automatic Semicolon Insertion is usually not a problem but it can occasionally bite you if you are unaware of it. What Automatic Semicolon Insertion does is really simple. It basically boils down to semicolons being optional in JavaScript and the parser  injecting them when it is appropriate. That might sound very nice, after all you can leave semicolons out and the right thing will happen. For example the following code, without a single semicolon, is completely valid and will print a sum of 3 as expected: 1: console.log(add(1, 2)) 2:  3: function add(x, … Continue reading X things every JavaScript developer should know: Automatic Semicolon Insertion

X things every JavaScript developer should know: Comparisons

Another item of things every JavaScript developer should know is how comparisons work. Just like with some of the other JavaScript, or I should really say ECMAScript, features anything you know about C# or Java could actually be misleading here.   To == or to === One of the weird things is there are actually two comparison operators in JavaScript, the double and the triple equals. The == is called the equals operator, see section 11.9.1 of the ECMAScript standard, and was the original equality operator. Unfortunately the way this operator works is quite some cause for confusion and as … Continue reading X things every JavaScript developer should know: Comparisons

Converting the RavenDB Northwind database to a more denormalized form

In a previous blog post I demonstrated how to denormalize the RavenDB sample database and use the DenormalizedReference<T> and INamedDocument types from the RavenDB documentation to make life really sweet. That leaves us with one small problem and that is that the original sample database doesn’t work with our improved document design. With the sample database, small as it is, loading all document as a dynamic type, converting them and saving them would be easy enough but in a real database that would not be practical. So lets look at a better solution fixing the database.   Updating the database … Continue reading Converting the RavenDB Northwind database to a more denormalized form

Denormalizing data in RavenDB

One of the things with RavenDB, or NoSQL document databases in general, is that you don’t do joins to combine data. Normally you try to model the documents you store in such a way that the data you need for most common actions is stored in the document itself. That often means denormalizing data. When you first get started with document databases that feels strange, after all with relational databases we are taught to normalize data as much as possible and not repeat the same values. Where normalizing data is great for updates and minimizing the size of databases it … Continue reading Denormalizing data in RavenDB

Tracking dirty objects in AngularJS

Tracking if an object is changed or not in AngularJS is quite easy but is also part of the UI so not always completely obvious. If you want to see if there are changes the $scope or the model will not tell you. Instead you need to take a look at the ngForm FormController. It has a $dirty flag that will tell you if an object is dirty or not. Saving that to the model itself is really easy, just use an ngForm directive, and the form element is automatically an ngForm directive, and the FormController will be added to … Continue reading Tracking dirty objects in AngularJS

X things every JavaScript developer should know: Truthy and falsy

One thing that developers often confuses with JavaScript is Boolean logic. It seems to simple, you use for example an if statement and put a boolean expression in there and if it is true the block of code is executed other wise the else block is executed. Something like this: 1: (function () { 2: var data = []; 3:  4: if (data.length > 0) { 5: console.log("The data array is not empty"); 6: } else { 7: console.log("The data array is empty"); 8: } 9: }());   And if you run it it will do exactly what you would … Continue reading X things every JavaScript developer should know: Truthy and falsy

X things every JavaScript developer should know: use strict

In the previous blog post I explained that you should normally use an Immediately-Invoked Function Expression (IIFE) to give variables function scope. That works fine as log as we remember to declare our variables using the var keyword. However when we forget the var keyword we are back to the original problem as the variables are added to the global scope.   Leaking undeclared variables As you can see in the two code snippets below the text variables are not declared using var. Even though they are only used inside a function they are still added to the global scope. … Continue reading X things every JavaScript developer should know: use strict

X things every JavaScript developer should know: scoping

I see a lot of developer with a C# or Java background adopting JavaScript as well these days. Not that it should be a big surprise, lots of business applications are developed as client side browser based applications these days. And like it or not JavaScript is the language of the browser. Unfortunately the JavaScript syntax resembles that of Java or C# resulting in these developers thinking that they can easily master the language. And even though JavaScript is not a hard language to master there are some important differences and gotchas everyone needs to be aware of. So on … Continue reading X things every JavaScript developer should know: scoping