Archive for the ‘Uncategorized’ category

Tip for razor pages not refreshing on reload with Service Fabric

July 7th, 2017

If you are having issues with your razor pages only refreshing when you start/stop the project here is a tip I found.

First go to your Service Fabric project properties and change the Application Debug Mode to “Refresh Application (Preview)” as detailed in this blog post: https://blogs.msdn.microsoft.com/azureservicefabric/2017/04/17/speed-up-service-fabric-development-with-the-new-refresh-application-debug-mode-2/

Then you have to make sure before you fire up your application to change the nodes to 1.  You can do this after you have installed the Service Fabric SDK and then right clicking on the tray app it installs and choosing “1 node”.  You have to be running in single node for the Refresh feature to work.

Hope this helps someone!!

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

April 3rd, 2015

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

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.

Downloading files with Javascript with post data without using ajax calls

May 21st, 2013

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

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.");
                },
            });

Error trying to run C# SDK for Couchbase

February 20th, 2013

If you are going through the tutorial on this page: Couchbase Tutorial and you get an error right off the bat trying to instantiate the CouchbaseClient with just a simple call like: var client = new CouchbaseClient(); then you probably have your app.config in the wrong order. Contrary to what they are saying on the couchbase site, refer to my config file below to get your stuff up and running correctly.

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="couchbase" type="Couchbase.Configuration.CouchbaseClientSection, Couchbase"/>
  </configSections>
  <couchbase>
    <servers bucket="default" bucketPassword="">
      <add uri="http://127.0.0.1:8091/pools"/>
    </servers>
  </couchbase>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
  </startup>
</configuration>

The big gotcha is the where I had the runtime line placed, if I had it at the top, it would throw the error, but if you have it as I am showing here, it seems to work fine.

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

February 5th, 2013

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.

Nice Javascript function to format numbers to currency

January 29th, 2013

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>

A very nice tool to display drop down data on your website

September 18th, 2012

I had a requirement on a project I have been working on to provide a search tool that can look up live data and filter it based on what you type through an auto-complete function.  I have been through 3 iterations, here is what I have used to this point:

The first one I used was the JQuery ComboGrid found HERE.  This tool was a good first run, it provides auto-complete and will display a grid that attaches to your INPUT element and allows you to display columns of data and will populate the field with whatever you select.  The problems I had with it is that it doesn’t look at all on an IPad and formatting it was somewhat of a nightmare, so I had to ditch this one.

Then I came across the Chosen control found HERE.  This control was a better step in that it does a very good job of formatting a SELECT element with auto-complete and filtering of the results.  The problems I ran into with this control was that it does some weird things on the IPad and it had no native function to hook into live data on a server, so I had to continue my search.

I have finally landed on the Select2 control, found HERE, so far this control does everything I need, it provides an ajax: function that allows you to hook right into your web call and even provides hooks for formatting the results and CSS file inclusion.   A word of advice if you are using the ajax: function, make sure you are using an INPUT element instead of SELECT and that your results include a lowercase id and text property, the control uses the id for searching, and the text property to display the results.  Happy coding!!

Are you writing LESS CSS?

September 11th, 2012

If you are a web developer and you write a lot of CSS, which chances are you do, then you need to be writing it using LESS.  The website to download LESS is here: http://lesscss.org/ and using it is really simple,  just include it in your site like so:

<link rel="stylesheet/less" type="text/css" href="styles.less">
<script src="less.js" type="text/javascript"></script>

And the Less Compiler will do the rest for you.  When you have all of that in place, you can starting writing your LESS CSS.

Less Files have a .less extension and you just include them in the same directory that you have your css files in.  The beauty of using LESS is that you can mix it with standard CSS, it will recognize both, so you can refactor your CSS alongside the LESS syntax, it doesn’t care!!

You can read all about it over on the link I have provided but basically you can create Variables, Mixins, Nested Rules, and even have operations on things like color and height and widths, basically a simple programming language for CSS, pretty sweet huh?

If you need a starting point to get going with LESS, I recommend going over and downloading the ELEMENTS.less file here: http://lesselements.com/  It is basically a generified LESS file that takes a lot of the standard things you would do in LESS and puts them into a file to save you time.  I am also linking here my edited version of my elements file where I added a few more things if you would like something a little more robust:  https://github.com/eddie1459/elements