24. September 2010 15:01
When using JavaScript these days, it is generally a good idea to namespace your javascript methods so that they don't polute the global namespace.
if (typeof mysite == 'undefined') var mysite = {};
mysite.section = mysite.section || {};
mysite.section.subNamespace = mysite.section.subNamespace || {};
mysite.section.subNamespace.component = (function(){ ... }());
This allows you to create a clean separation of your utilization. Although you may not want to go as deep as the example above, you can see
how this could very well become cumbersome when you want to declare one or more namespaces. It would be nice to have a helper method that
lets you simply declare namespaces.
//declare a single namespace
namespace('mysite.section.subNamespace.component');
//declare multiple namespaces at once
namespace('mysite.section2', 'mysite._utilities');
This works out much nicer, and is easier to repeate as-needed when creating your namespaces. The function I am using for this is below.
var namespace = (function(root){
//regular expression to limit formatting of namespaces
var nsre = /^([\$\_a-z][\$\_a-z\d]*\.?)+$/i
//define returned function
return function(ns) {
var args = Array.prototype.slice.call(arguments);
var ret = [];
while (args.length) {
ns = genNS(args.shift());
if (ns) ret.push(ns);
}
if (ret.length == 0) return; //undefined, no valid input
if (arguments.length == 1) return ret[0]; //only a single input, return that namespace
return ret; //used overload for multiple namespaces, return the array/list
}
//private static method to generate a single namespace
function genNS(ns) {
if (!ns.match(nsre)) return;
ns = ns.split('.');
var base = root;
for (var i=0; i<ns.length; i++) {
base[ns[i]] = base[ns[i]] || {};
base = base[ns[i]];
}
return base; //return resulting namespace object
}
}(this));
This functionality is very useful, and is included in a number of API toolkits.
You could replace the var namespace declaration and attach it
to an existing object such as $.ns, which would
attach it to an existing reference.
If you have suggestions for future topics, feel free to leave a comment or
contact me via email.
1. April 2010 15:19
I'm currently working on a project where one of the applications has a subform, or child form that has interactions that are separate from the main page within the application. The main page is essentially a filter form, with a results grid. Each item in the grid displays a child form, when the child form is completed, the original screen is displayed again.
Sounds simple enough right? Well, the business desire is to have the filter option form keep its' settings when returning to the page. My initial solution was to use a jQueryUI dialog based option (via an IFrame). Which works great, except in certain conditions IE7 flickers when the mouse moves in/out of the IFrame itself if there are scroll bars present. ugh.
I didn't want to use cookies, or server-side session state as these will affect all windows using the main form. If a user launches the app in a new window, with different filters set, I didn't want these windows to effect each other. Then it occurred to me, I could use window.name to store the state of the form when entering, and leaving the page. I tend to store an "__original_value__" for each form element when a page loads, that way its' easy enough to return to default values later on via code. More...
23. February 2010 19:08
Okay, so I wanted to return a paged result set from a Stored Procedure in Microsoft SQL Server in T-SQL.
The results in question can be easily fed via a web service endpoint to a Silverlight, DHTML or other dynamic
grid. My own use is to populate a jqGrid.
The features I need are to be able to return only the relevant results for the current page, as well as a
count of the total rows available. I need to be able to input the current page number, an arbitrary page
size, as well as dynamically sort on a given column. My real world use is a fairly complex set of joined
tables, with several input parameters, so I am going to limit me queries to one table with the following
format:
CREATE TABLE MyList (
[id] UNIQUEIDENTIFIER PRIMARY KEY DEFAULT NEWID(),
[created] DATETIME NOT NULL DEFAULT GETDATE(),
[name] NVARCHAR(100) UNIQUE NOT NULL DEFAULT '',
[description] NVARCHAR(250) UNIQUE NOT NULL DEFAULT ''
);
More...
11. November 2008 22:14
My goal is to create a personal website application that implements the features needed, and wanted for someone who wants to create a personal site that extends beyond just a blogging engine. I've been wanting to work with ASP.Net MVC and some other technologies, so I'll be focusing on using them in MySite. More...
1. October 2008 14:59
Today I had to create a FAQ page. In order to make this work nicely I started with a simple Dictionary List markup, and some jQuery sugar to make an expand/collapse list. Although I love prototype.js, jQuery's nesting and inline DOM object creation really comes in handy. More...