Mikael's blog

A developers seventh time trying to maintain a blog

Tag #architecture

Client-side JavaScript Initialization | December 7, 2011 at 21:37

Yesterday I started using the [BundleController](http://lofjard.se/post/automatic-minification-and-bundling-with-nodejs) with my JavaScript files on the client-side. I also deployed my [ViewManager](http://lofjard.se/post/handling-views-in-nodejs) so that all my views now uses the bundled and minified JavaScript file produced when I start (or restart) my Node.js server. This meant that all my JavaScript was being run on every page in the blog, and while it isn't that much JavaScript yet I still wanted to improve on it. I decided to make use of some closure magic and built something like this: ````js if (typeof(BlogPageFunctions) == 'undefined') { BlogPageFunctions = {}; } BlogPageFunctions.admin = function () { // query some objects with jQuery here, // bind up some events etc.

Architecture in JavaScript - Closures | December 6, 2011 at 21:53

Today is Tuesday, which for me means I've held another tutorial at work. Today it was less hands-on and more of me just talking and showing examples on the projector using the wonderful [jsfiddle](http://jsfiddle.net/) JavaScript quick-hack-tool. My talk today was mostly about closures, so I thought I should write something here on the subject as well. I find that it's not that easy to explain closures using just words, but if I were to try it would go something like this. * A closure is a functions variable scope that still exists after the function has executed. * A closure only exist as long as something else has a reference to something that needs it. * A closure is created when an outer function has inner functions. If you have no idea what I meant with any of that, then I hope th

Architecture in JavaScript - Inheritance | November 29, 2011 at 21:40

There's been a lot of debate over the years about whether JavaScript is an object-oriented language or not. Most of these discussions seem to pan out into arguments over inheritance. JavaScript, by design, lends itself well to inheritance through extension. However, JavaScript can support lots of different types of inheritance. I'm going to talk about a few of them today. Consider the following code: ````js function Car() { this.color = "Blue"; this.brand = "unknown"; } function Ford() { var o = new Car(); for(var i in o) { this[i] = o[i]; } this.brand = "Ford"; } var myCar = new Car(); var myFord = new Ford(); ```` The `myCar` object would have the properties `color` and `brand`. The `Ford()` constructor method creates inheritance by creating a `Car` and copying a

Node.js Development Server Strategy | November 24, 2011 at 17:28

When I started building this blog my server was the production environment and my laptop was the development environment. This all changed when I added the database and started doing more UNIX specific stuff such as path resolving and such. So from then until yesterday my production environment was also my development environment. Not the best strategy I admit. Yesterday when I rewrote the architecture I also decided that I needed to set up a separate node.js instance as a development environment on the server. The first problem I had to solve was configuration. My configuration was scattered all over the code base so I moved it all into `index.js` which is the file I execute with node when I start the server. I also did not want to have to change my configuration before I deploy (copy)

Architecture in Javascript - Modules | November 24, 2011 at 11:14

About a week ago I wrote about [turning client side JavaScript into CommonJS modules](http://lofjard.se/post/the-power-of-commonjs). The scoping of variables in CommonJS makes this easy, but what if we wanted to do it the other way around? In CommonJS, modules are scoped by file and all public methods and properties must be propagated through the <code>exports</code> object. ````js var foo = 'foo'; function bar() { return foo; } exports.bar = bar; ```` In the above example the `foo` property is private to the CommonJS module and can only be read from the outside by the public method `bar()`. If we were to include this JavaScript file on the client however, the property `foo` and the method `bar()` would both exist in the same scope as all other JavaScript files. What we need is to

Overhaulin' | November 24, 2011 at 01:08

Tomorrow (actually today when I write this) my daughters day care is closed and I get to spend the day with her at home. That meant that I could stay up late tonight and do some work on the blog. The last few days I've felt that the file sizes were beginning to get out of hand. This easily happens when you're writing JavaScript since it's kind of a cowboy language. I love that I am able to twist and bend the language to my will, but the same thing that makes JavaScript so powerful can also be its worst enemy. It's way to easy to loose control of yourself and before you know it you end up with a 1000-lines-of-code monster file and your head starts spinning every time you want to make a change to it. So today I rewrote the blog. Not all of the logic, but basically all of the architectural

Why node-static is a good idea | November 22, 2011 at 21:40

If you are using node.js as a web server (as I am) and you want to serve up static files there are a lot of ways you can go about doing that. I'm going to talk about 3 of them. ## The bad path: Serving them yourself It's really easy to do this for a proof of concept. Node.js has an easy to use `fs` library for reading and writing files to the file system. In order to serve up some HTML page you just parse the request URL, retrieve the file using `fs.readFile(...)` and then write the file to the response stream. I did this for the small server I use at my Tuesday tutorials at work. The problem we faced last Tuesday was that our web site would not load properly in IE9 without resorting to "compatibility mode". At the time I didn't have time to look into it (as I was tutoring) so I did it

Sorry, sharing is not available as a feature in your browser.

You can share the link to the page if you want!