Hide A DOM Container Clicking Outside It Using jQuery

Let’s say we have a DIV container with some content in it. We want to hide that container if the user clicks outside its bounds. Pretty simple solution:

$( document ).mouseup( function( e ) {
    if( $( "#myDiv" ).has( e.target ).length === 0 ) {
        $( "#myDiv" ).hide();
    }
});

#click, #container, #div, #dom, #hide, #jquery

Programmatically Animated Page Scroll with jQuery

With the increasing use of AJAX to populate a page without reloading it, you come to find out that you’d like to get your users back to a certain point in the page, if not the top. If you where to navigate to a new page via the browser, you always end at the top. But when just replacing a container with new information this doesn’t happen. Depending upon your scenario, this may be a good idea for usability.

The following code snippet is a simple example. It will detect if your user is below 210 pixels on the screen. If you are then it will animate the page, scrolling it up to the 210px mark. You may want to use an extended jQuery .position() method to find the top of your #result container instead of using a static page height value.

$.get( 'ajax/test.html', function( data ) {
    $( '#result' ).html( data );

    if ( $( window ).scrollTop() > 210) {
        $( "html, body" ).animate({
            scrollTop: 210
        }, "slow" );
    }
});

#jquery, #scroll

I ran into Muhammand’s blog on decreasing row count cost and thought it was a great tip to use in the future!

Muhammad Imran's avatarSQL Server Portal

Today, we will discuss the efficient way to count records of any table. Lets suppose you have millions of records in a table. How will you calculate the record count quickly ?

Let me explain this with simple examples :

Example 1 :
First, lets use the traditional way to count records from the table.

As per the results, the above query took almost 26 seconds.

Example 2 :
Lets go to SSMS to view how SQL Server calculates the record count.

Right click on the table and go to properties. It will give you a lot of information including record count in a fraction of seconds.

Now, we need to find out what query is running behind these properties. So lets open the SQL Server profiler. A lot of queries were running to calculate different information but I grabbed the query that calculates record count.

Given below is…

View original post 57 more words

Removing Duplicates in MS SQL 2005+ Table w/o Adding an UID

Let’s say we have a table that links products to a category:

productID AS INT
categoryID AS INT

For some reason we managed to get dulicate productID’s and categoryID’s. I don’t really care to add a unique ID (UID) field to reference against using this script:

ALTER TABLE productCats
ADD RowID INT IDENTITY(1,1)
GO

DELETE productCats
FROM productCats
LEFT OUTER JOIN (
    SELECT MIN(RowID) AS RowID, productID, categoryID
    FROM productCats
    GROUP BY productID, categoryID
) AS KeepRows ON productCats.RowID = KeepRows.RowId
WHERE KeepRows.RowId IS NULL
GO

ALTER TABLE productCats
DROP COLUMN RowID
GO

Here we group together the duplicates into one row and assign it the minimum RowID for that group. If the left join returns a NULL when comparing all the RowID’s to the new set of groups then it will be deleted.

In MS SQL 2005+ I can use a Common Table Expression (CTE) to solve this issue:

WITH CTE
     AS (SELECT ROW_NUMBER() OVER (PARTITION BY productID, categoryID ORDER BY ( SELECT 0) ) RN
         FROM productCats)
DELETE FROM CTE
WHERE RN > 1

In this example, we give a row number (RN) to each group of duplicates and delete that row if greater than 1.

For more information on CTE’s see http://msdn.microsoft.com/en-us/library/ms190766(v=sql.105).aspx

#cte, #duplicates, #sql, #t-sql

Missing ColdFusion 9 Solr Collections List

After trying to track down a slow server issue, I noticed that the Solr Collections list in the ColdFusion Administrator did not have the usual list including “core0”. I’m not sure if this list ever existed as this is a new multi-server instance setup by another person. However I did know something was wrong.

So my first step was to locate the collections directory for this instance and delete everything. Wrong thing to do.

Taking a look at the “C:\JRun4\servers\cfusion\cfusion-ear\cfusion-war\WEB-INF\cfusion\solr\logsstderr-????_??_??.log” I see I’m now getting “SEVERE: java.lang.RuntimeException: Can’t find resource ‘solrconfig.xml’ in classpath or ‘C:\JRun4\servers\…\cfusion.ear\cfusion.war\WEB-INF\cfusion\collections\…’, cwd=C:\JRun4\servers\cfusion\cfusion-ear\cfusion-war\WEB-INF\cfusion\solr” when I run my page script. The script detects if the collection exists; if not then create it; then populate the collection.

So now I’m thinking that the collection list probably exists in an XML config file somewhere, but is somehow corrupted as the ColdFusion Administrator can’t read it.

After doing some searching I find the “core” list in “C:\JRun4\servers\cfusion\cfusion-ear\cfusion-war\WEB-INF\cfusion\solr\multicore\solr.xml”. The XML has multiple core children elements under the “solr\cores” path which include the standard “core0” and my missing collections. Because I can just delete those collections, recreating them problematically later, I decide to delete all core elements except “core0/”. Not sure if I need to restart anything or not, I restart the SOLR service and the ColdFusion instance.

I now see “core0” in the ColdFusion Collections list as normal and am able to create collections normally as well.

#coldfusion-2, #collection, #solr

Returning Top SQL HierarchyID Ancestor

In an attempt to optimize a query that is taking me 10 seconds, I was looking for any way possible to shave off time.

The result will populate into a SOLR collection, and we narrow results based upon the top category ID. For example a product in the category “/7/15/92/” would be give a category of “7” in the SOLR collection. Thus we could do a search for a specific belt inside all vacuum parts for example.

In order to achieve this, our first attempt was the following:

REPLACE( SUBSTRING( HID.ToString(), CHARINDEX( '/', HID.ToString() ), CHARINDEX( '/', HID.ToString(), 1 + CHARINDEX( '/', HID.ToString() ) ) ), '/', '' )

This worked great, but I noticed that it was a bit intensive. So I did a little more research and found a method that shaved a second off of the result time:

REPLACE( HID.GetAncestor( HID.GetLevel() - 1 ).ToString(), '/', '' )

I actually couldn’t find this example anywhere and thought it up for this use. If you have a better method, feel free to comment. Thanks!

#ancestor, #hierarchyid, #sql

ColdFusion Implicit Struct Case Sensitivity For JSON

I’ve always learned that in ColdFusion 9+ that dot notation and implicit structs will always convert the key into uppercase when returning it as a JSON string.

To get around this you would always use bracket notation.

However I learned today, by example, that if you enclose the defined key, in an implicit struct, inside quotes that the key case will be preserved.

// DOES NOT PRESERVE CASE
// returns: {"MYKEY":"myValue"}
myStruct.myKey = "myValue";
return myStruct;

// returns: {"MYKEY":"myValue"}
myStruct = { myKey = 'myValue '};
return myStruct;

// PRESERVES CASE
// returns: {"myKey":"myValue"}
myStruct[ "myKey" ] = "myValue";
return myStruct;

// returns: {"myKey":"myValue"}
myStruct = { 'myKey' = 'myValue '};
return myStruct;

#case, #coldfusion-2, #json

Application.cfc onRequestStart() Called Twice

I spent a bit of time tracking down why I kept seeing two exact same threads (run by thread()) being run simultaneously. These threads are spawned by FrameWork One’s (FW/1) setupRequest() method. After tracing the methods with some console dumps, doing some research via Google and process of elimination, I finally got a clue.

Let’s say I hit http://mysite.com and this site requires a login. If you are not logged in lets saw we use a location() call to get to the login page. I also use a log-in imitator via a UI to see how different account look under their login, which also uses the location() method. Because of these location() calls, the onRequestStart method is called once before the redirect and once after the redirect.

Now, thinking I can overcome this by wrapping the thread() call in if( !isDefined( “myThreadName” ) ). However – apparently the onRequestStart calls come so quick that the thread name isn’t created in time to be detected. So two threads are still spawned.

I could probably add a flag to the session scope wrapped in a lock operator. However what happens if the thread is terminated manually? This may not be the best option, but it’s not bad as long as your site admins are aware of that consequence.

In this case I’m going to reassign my statements to my log-in handler instead of my setupRequest() method and call it good.

Let me know if you have any other ideas on how to handle this. I’m all ears.

Accessing JavaScript Methods From Outside Its Self-Invoking Function Wrapper

Let’s say you have a method you want to be globally accessible, but want a self-invoking function around it to make sure it’s jQuery $ alias safe.

In this example, the following code is in global.js:

(function( $ ) {
   function showLoader( cfg ) {
      if( cfg == 'show') {
         $( '.loader' ).show();
      } else //more code...
   }
})(jQuery);

In this example, the following code is in myPage.js:

(function($) {
   showLoader( 'show' );
})(jQuery);

You will find that myPage.js cannot access the showLoader() method even though global.js was called first in the HTML page.

The fix for this is to push the showLoader() method out to the window (global) scope:

(function( $ ) {
   window.showLoader = function( cfg ) {
      if( cfg == 'show') {
         $( '.loader' ).show();
      } else //more code...
   }
})(jQuery);

By assigning the function to the window scope it is now globally accessible. Just remember that it is now no longer protected by the closure but it still has access to the jQuery $ alias.

NECFUG FW/1 Presentation

In July 2012, I presented to the Nebraska ColdFusion User’s Group on the topic of FrameWork 1 (FW/1) for ColdFusion. Thanks to Ryan Stille for recording this.

Also be sure to check out “A crash course in MVC with FW/1” with Steven Neiland as this provides a much better method for presenting this topic.

Continue reading