Correct way to connect to mySQL from Node.js (at least for me)

April 3rd, 2015 by admin No comments »

Here is the code I used to connect to mySQL from node.js using this library: https://github.com/felixge/node-mysql/

I know he constantly refers to

connection.connect();
and
connection.end();

But I just simply didn’t need those, I do use RedHat openshift for my project so your mileage may vary.

var express = require('express');
var mysql   = require('mysql');

var connection = mysql.createConnection({
  host     : 'MYSQL_DB_HOST',
  user     : 'MYSQL_DB_USERNAME',
  password : 'MYSQL_DB_PASSWORD',
  database : 'dbName'
});

//then down in your app

app = express.createServer();
app.get('/login', function(req, res) {
            connection.query('SELECT * FROM `Users`', function (error, results, fields) {
                  res.send('Results: ' + results.length);
            });
        });

Updating SVN and losing permissions on windows machine?

January 8th, 2014 by admin No comments »

Are you running a project on windows and each time you update your repo locally from SVN losing permissions on files?  I have banged my head around on this one for quite a while and I think I finally have the answer to this problem.

Open your repo on your machine and make sure you have hidden files turned on and look for a directory that is called .svn.  Right click on that folder and choose “Properties”, then choose the “Security” tab and inside that make sure you have the IUSR user in there, especially if you are running IIS as your web server.  If you already have that user in there or once you get that in there, click on the “Advanced” button below and then you will see the same users again in a screen that looks like this adv_sec

Click on the check below to “Replace all child object permission entries with inheritable permission entries from this object”.  Hit Apply and it should go thru and apply that to everything in your svn directory.

Apparently the problem is when you do an svn “Update”, it brings the new files down, sticks them into temp and then copies them over.  That copy somehow loses permissions during that process, maybe it brings the file into that tmp folder and then that file with less permissions comes over to your directory with less permissions.  Not sure but this seems to fix the issue for me.

My Bootstrap 3 notes

January 2nd, 2014 by admin No comments »

I thought I would put together a list of some things I have noticed about Bootstrap 3 for anybody here either trying to implement it for their site or doing an upgrade form 2.x to it.  This will probably be more helpful for those of you upgrading because I will go into differences I have noticed in the two.  I welcome any comments to this as I do not profess to be the authority on this stuff, lol.

Here is the code I came up with for the popover tool

var popOverSettings = {
                        placement: 'bottom',
                        container: 'body',
                        html: true,
                        selector: '#tools',
                        content: tpel,
                        title: "Tools"
                    };

                    $('body').popover(popOverSettings);

The key differences I see here is the container: body and the selector: ‘#tools’ items.  Those options seem to be in the last 2.x version but for some reason my popover would not format correctly till I got it added in.

Another big thing I noticed is that a lot of the elements across my site had user agent css applied to my html, in particular this one: -webkit-padding-start:40px;.  What you need to do to solve this one is add in a ‘reset’ css to your site up front, here is a good one to reset your elements before bootstrap gets a hold of it: http://meyerweb.com/eric/tools/css/reset/reset.css.  Supposedly these styles are just part of the browser, like Chrome or Internet Explorer and the reset.css file just takes your site and ‘resets’ all the elements back to the basic.

Handy jQuery snippet of the day (sum input fields)

June 19th, 2013 by admin No comments »

Here is a little handy piece of code I wrote that greatly simplifies summing values across a bunch of text inputs on your page, all you have to do is stick a class on the inputs you want to sum, in my case the class was ‘budget’ and voila, you will have the sum without having to do a bunch of this + that.

var budgetTotal = 0;
$('.budget').each(function (e, s) {
     budgetTotal += parseFloat($(s).val());
});

Downloading files with Javascript with post data without using ajax calls

May 21st, 2013 by admin No comments »

IF you have been using javascript for a while you will know real quick that you cannot use $.ajax calls to receive a file from your server, which is a real bummer due to the fact that ajax calls just stream down the text data. I searched the web far and wide trying to find a solution to this and if you just simply need to download a file from your server, there is a really simple way to get it accomplished, just simply write this line into your javascript:

location.href = “path/to/api/call”

That will simply go out to your server and execute that on the server and get your file, as long as the response on the other side has headers marked up correctly with the “Content-Disposition” of “attachment: filename=file.whateverext”.

That is fine for simple file download, but you know at some point you are gonna want to pass parameters to your web server so I found a great jQuery plugin here: jQuery Download plugin This will allow you to post your data to the server as a hidden form submit and it works great minus one little issue, it doesn’t handle nested array’s at all, it just wraps them into the post string, so I modified it a little and create a git repo for it enjoy: My Modified jQuery plugin

Backbone collection sorting.

May 14th, 2013 by admin No comments »

Recently I had a need to sort a list of records in a list by the Date it was created and I wanted to do so using backbone, well it turns out there is a very simple and slick way to get this done, I have included the code snippets below:

The collection:

define([
  'backbone',
  'Models/YourModel'
], function (backbone, model) {
    return backbone.Collection.extend({
        url: '/your_url/',
        model: model,
        sort_key: 'id', // default sort key
        comparator: function (item) {
            return -item.get(this.sort_key);
        },
        sortByField: function (fieldName) {
            this.sort_key = fieldName;
            this.sort();
        }
    });
});

As you can see you just use the comparator: function built into the backbone collections and in my example I am passing a negative so that it will sort descending, the default is ascending so just leave the minus sign off if you want it to work that way. I included the sortByField function here as a convenient way to call it in the code off the collection, it uses the sort_key field in the comparator to organize your collection. Below I have the usage, as you can see, its just a simple call to the string name of the field you want to sort by.

The usage:

new collection().fetch({
                data: { data: "whatever" },
                success: function (r) {
                        r.sortByField("FieldToSortBy");
                },
                error: function (c, r) {
                    alert("Error retrieving the data.");
                },
            });

Be very careful when you set the id of a Backbone model

May 10th, 2013 by admin No comments »

I have been scratching my head all morning today trying to figure out why a collection I had on my backbone model was getting reset anytime I passed it to my views.  I tried everything, passing the collection to my view in functions, instantiating it in the initialize(), and a few other things that I can’t even remember right now, lol.  Anyways, I noticed down where we were adding items to the collection there was a line that looked like this:

that.model.set(“id”, evt.val, { silent: true });

Notice the lowercase “id” there.  What that is telling backbone is to actually set the id of the model instead of just setting the id to something we had planned to reference down the road, which would be fine in most situations, although I think backbone will do a PUT in those situations because it thinks the model has been saved before.  Anyways, when this was set in my app, it was clearing the collection whenever I passed it around, which was not cool.  All I had to do was change that to an uppercase “Id” and update any reference I had to it and all is well again, my collection stays intact.  I hope this helps someone.

Value cannot be null. Parameter name: source

April 25th, 2013 by admin No comments »

Sometimes you may get this error when you are debugging a linq query and it may seem pretty vague to you, and it is, very.  In my situation I didn’t have a parameter name called “Source” or a property named that either, it turns out the problem was a property I had in my linq query that was in fact null and when you execute a query across stuff that may be null you can run into stuff like this.

If  you see this error when you writing code, the easiest way that I know to figure out your problem is to look into anything the query is grabbing that may have a null in it.  Just simply start commenting out stuff and see what happens.

Good luck and I hope this helps someone!!

Underscore.js attempts to read commented HTML

April 11th, 2013 by admin No comments »

I just wanted to throw this out there for anybody trying to figure out why their underscore templates may be failing with properties in their commented html.  It looks to me like underscore.js actually parses everything in the html file in certain situations, I am not fully sure yet but I couldn’t really find much on the web that spoke about it.  I thought I would throw this blog post up so anybody noticing this that has seen it before could chime in and offer some insight into this.  What is weird is that it doesn’t always do it, but only seems to when a change is made at some point to the template being rendered, very strange.  I guess for now my only alternative would be to remove the <%= %> in my comments.  If anybody has some alternatives I am all ears.

How to get all items from an HTML table with JQuery

April 9th, 2013 by admin No comments »

Here is a little snipped that saved me a lot of manual code writing if you are needing to iterate over a set of rows in a table you have on your webpage. Just simply use the “gt(0)” which according to the JQuery API documentation “select all elements at an index greater than index within the matched set.” Then just pair that with tr: in front of it to select all the table rows of your table and voila, you have all your rows. You can then use the .each method to do the iteration. Don’t forget to change #tableId to whatever you called it in your html, enjoy.

$("#tableId tr:gt(0)").each(function () {
    //perform logic here
});