Using Javascript closures in for loops to keep callbacks from overriding the iterator

February 5th, 2013 by admin No comments »

The problem: You have a for loop in javascript that is executing and you are losing your pointer to the iterator. You can write something like below, wrap your stuff in a closure like so:

var funcs = [];

for (var i = 0; i < 3; i++) {          
    funcs[i] = (function(index) {   
        return function() {          
            console.log("My value: " + index);
        } 
    })(i);
}

Since there is no block scope in JavaScript – only function scope – by wrapping the function creation in a new function, you ensure that the value of “i” remains as you intended. This drove me crazy till I understood how things work in Javascript, now life is good, hope this helps someone.

The PouchDb project is looking pretty good

February 1st, 2013 by admin No comments »

I along with a co-worker have been following the PouchDB project, HERE is a link to the project’s website. If you have been following some of my posts in the past, you know I have dabbled and used CouchDB quite a bit. One of the issues we ran into was when you want to post your documents right into a local couch say sitting on port :5984, you will run into a Cross Domain error, well, PouchDB seems to alleviate this issue by being a native JavaScript NoSQL solution, how sweet is that? The even sweeter piece to this puzzle is that it syncs right to a couchdb database natively, which is the icing on the cake for me. The big issue with PouchDB in the past was that it had a version of JQuery baked into it, which was causing any app I had that used JQuery to step on itself because of the multiple references, well, the guys working on that project have seemed to fix this problem, this tool is starting to look ready for the big time. I will update in the weeks to come some scenarios/examples of this in action.

Nice Javascript function to format numbers to currency

January 29th, 2013 by admin No comments »

Here is a code snippet I use to take any number in Javascript and format it nicely to a currency, if you just want it to be a regular number, simply leave off the ‘$’ in the return line, voila!! It may look pretty garbled below, if you click on the ‘<>‘ View Source it will show you what its supposed to look like.

Number.prototype.formatMoney = function (c, d, t) {
                var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "", i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
                return s + '$' + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(d{3})(?=d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
            };

BTW, you can use this in any standard JavaScript call, even in Underscore templates like this:

<label><%= YourNumber.formatMoney(2, '.', ',') %></label>

Cool OSX tip of the day.

January 25th, 2013 by admin No comments »

I do a lot of video editing with my mac and whenever I create a video for upload on the web I always need a still image for the preview image in the jwPlayer I use on the web. I used to have to have some kind of program to grab the still and export it for me, not anymore. This functionality is built right into OSX, all you simply have to do is do a SHIFT-COMMAND-4 and it will bring up a crosshairs and allow you to select the item you would like to capture and immediately turn that into a png file and place it on your desktop, how cool is that?

My first little Node.js App

January 24th, 2013 by admin No comments »

If you are a developer and you haven’t heard about node.js, what rock have you been living under??  Anyways, if you have been hearing about it and would like to learn more, head over to their site HERE and download it and start playing with it.  Its super easy to get going with node, once you install it, all you have to do is open a terminal window and type node “name_of_file.js”, with the correct javascript in it, of course.

I just wrote up this quick little example to see if I could get node to act like a webserver, so here is the js code for it below:

var http = require('http');
var fs = require('fs');
http.createServer(function (req, response) {
    fs.readFile('index.html', function (error, content) {
        if (error) {
            response.writeHead(500);
            response.end();
        }
        else {
            response.writeHead(200, { 'Content-Type': 'text/html' });
            response.end(content, 'utf-8');
        }
    });

}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

Just create a directory anywhere on your system and create a file with index.html (That’s my example, it can be anything) and then simply issue this command in the terminal window.

node “name_of_file.js”

My example is this: node server.js.

You will get a ‘Server running at http://127.0.0.1:1337/’ message after that and that will let you know that node is running correctly, all you have to do after that is open a web browser and go to the address of http://127.0.0.1:1337 and you should have the webpage you built, you may want to just slap some text in there so you see something.

I hope this helps someone, I plan on delving into the node realm a lot more in the coming weeks, so look for other posts down the road.

Help!! I have reset my Ubuntu 12.10 password and now the keyring prompt keeps coming up!

January 18th, 2013 by admin No comments »

I had recently forgotten my Ubuntu login and figured out how to reset the password by basically following these directions found HERE. Then when my system comes back up, I am getting a screen like this:  
The method I used to fix this on my system was simple, just open your directory and hit CTRL+H and that will show the hidden files and folders and then just simply navigate to your /.local/share/keyrings folder and delete anything you have in there.  This prompt won’t bother you again.  I hope this helps someone!!

Installing Chrubuntu on SD Card (to avoid messing with your Chomebook factory settings.)

January 17th, 2013 by admin No comments »

In a previous post, I posted about putting Ubuntu on your ARM based Chromebook, well if that seems too risky for you, Jay Lee, the guy at this blog post, has created a script to put the OS on a SD card and allow you to dual boot it or ChromeOS, which is exactly what I have been looking for. My chromebook just keeps getting more functional!! If you would like a more visual approach, here is a nice blog post with some nice pictures he’s provided to kinda let you see what you are doing, enjoy.

Create a dynamic and searchable optgroup with select2

January 16th, 2013 by admin No comments »

If you are writing a web app, you may or may not know about the Select2 control found HERE. I posted about it a few months ago here: http://www.eddiedillon.info/?p=348 and I was going to give you an update with an example of how to use it with an underscore template to create a searchable select with grouping. This will be useful in a project I am working on and I thought I would share. I have attached my jsFiddle below, the creator has an example of templating you can find there but I couldn’t really find an example of how to load the data in dynamically and you CANNOT use the ajax function in it with a select, so basically I create the entire select up front then style it.

How to place latitude and longitude points on a OpenLayers Map.

January 14th, 2013 by admin No comments »

I have posted a jsFiddle for anyone interested in displaying latitude and longitude points on an OpenLayers Map, I just found some random points from the area around where I live and stubbed them in there, enjoy.

Visual Studio 2012 Tip of the day

January 12th, 2013 by admin No comments »

I have multiple screens when I am at work so when I take my laptop home I like to work on that and as a result I have issues with windows not showing up on the screen. If you are like most Visual Studio users you make heavy use of the ‘Find in Files’ tool (CTRL+SHIFT+F), its a very handy tool. Well, if you run multiple monitors and you try to find it, it will more than likely be somewhere else and you won’t be able to find it. If you would like this window to show up, it’s gonna cost you some personalization to your studio environment, but it was worth it to me, so I went ahead and did this to get my find tool back:

Just simply Navigate to Window > Reset Window Layout, and agree to the Are you sure you want to…? prompt, that will reset your docked windows but will bring back these floating ones, but these little floating windows are pretty valuable IMO, I hope this helps.