Jim Rogers

Lives in Baton Rouge, LA, with two dogs, one cat, and one lovely wife. I'm a lead developer for GCR Incorporated.

Katrin and Jim

Month List

jQuery Mobile - issues navigating to root

by Jim Apr 12, 2013 3:04 PM

When I load the default index.html page of my jQM site, I make an ajax call and dynamically show a couple of buttons. After navigating to a subpage, I can get back to the cached index.html by navigating to "index.html", either through a link or programmatically with $.mobile.changePage().

So far so good.

Now if I load the same site in IIS, without specifying the index.html: http://localhost/, I run into problems. "index.html" is not in the original url, so jQM thinks it's loading a new page. I retrieves the page from the server and inserts this into the DOM - but this time, without the dynamic content being created.

I've got a bunch of links to index.html, and I don't want to programmatically change each according to how the site is initially loaded. This hook into the pagebeforechange event will fix the url before navigation starts, and both use cases work seamlessly.

// hook into navigation to fix issue with blank root navigation vs. index.html
$(document).on("pagebeforechange", function (event, data) {

    // event is called twice, ignore the second object call
    if (typeof data.toPage == "string") {
        var target = data.toPage;

        // are we going to index, but that's not in the original path?
        if (target.toLowerCase().indexOf("index.html") > 0 &&
            location.href.toLowerCase().indexOf('index.html') == -1) {  

            // change to the root of the site instead of to index.html
            target = location.protocol + "//" + location.hostname + location.pathname;
            if (target.substr(target.length - 1, 1) != "/")
                target += "/";
            
            data.toPage = target;   // continue on with this new value
        }
    }
});

Tags:

Code

Generating .NET classes for XML deserialization

by jim Mar 15, 2013 2:15 PM

I'm not the world's biggest fan of XPATH queries, and they get tedious real fast if you need to search through sets of nodes in XML. It's much easier to deal with class objects. If you've got XML you want to deserialize, and need the class definition to do it, Microsoft has you covered:

  • Create a new XML file in Visual Studio, and paste your XML into it
  • On the XML menu chose "Create Schema"
  • Save the generated XSD file to a convenient location, like c:\temp\bing.xsd
  • Open a Visual Studio command prompt at this location - this is easier if you have the shell extension :-)
  • Run the xsd.exe tool to generate class files

C:\TEMP>xsd.exe c:\temp\bing.xsd /c /out:"c:\temp" /l:VB
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 4.0.30319.1]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file 'c:\temp\bing.vb'.

C:\TEMP>

The generated code is far from ideal:

  • I don't need most of the properties
  • The class names are cumulative, and not intuitive, like ResponseResourceSetsResourceSetResourcesLocationGeocodePoint for the GeocodePoint element
  • Properties unnecessarily use a field backing store
  • Elements that appear once under the parent are assumed not to be collections - but I know in my case that some should be - i.e., they appear once just for this particular query, but may appear multiple times in others

But it's a good starting point for refactoring.

Tags: ,

Code

NuGet - package download on the build server

by Jim Feb 22, 2013 7:24 AM

I'm getting my NuGet foo on, and automatically downloading any missing packages at build time, as opposed to committing them to source control.

imageOf course I want this to work on the build server as well. The build doesn't happen under my account, and in any case NuGet is not installed into Visual Studio on that machine. But we don't need that! The NuGet executable is included in our solution, and checked into source control, when we "Enable NuGet Package Restore" on the solution.

Per the instructions, on the build server, set a new environment variable:

  • In the start menu, right-click on computer, select properties, then Advanced System Settings, Environment Variables
  • Create a new system environment variable called EnableNuGetPackageRestore, with the value "true"
  • Reboot the build machine so this will take effect for the build account

Works like a charm.

There's a new source folder with every build, and this presumably means that the build server will download the packages for every build. This isn't a big deal, but if it were a problem, it would be possible to set up and configure a package source on the local network, and use that instead of the default online source.

Tags:

Code

Resolving those secondary references on the build server

by Jim Feb 01, 2013 4:50 PM

There's a difference between how secondary references are resolved when building within Visual Studio, and how they're resolved on the build server. So a solution that builds fine locally might fail on the build server with an error similar to this:

Reference required to assembly 'System.ServiceProcess, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' containing the base class 'System.ServiceProcess.ServiceBase'. Add one to your project.

There are a number of potential solutions to this, but one of the cleanest is described on stack overflow and on this blog. I kept getting this error when compiling locally, though:

A problem occurred while trying to set the "References" parameter for the IDE's in-process compiler. Error HRESULT E_FAIL has been returned from a call to a COM component

The final solution for me was to add the following to the offending project files, right at the end, just above the closing project tag:

<Target Name="AfterResolveReferences"
<!-- Redefine referencepath to add dependencies-->
<
ItemGroup Condition=" '$(BuildingInsideVisualStudio)' != 'true' ">
<
ReferencePath Include="@(ReferenceDependencyPaths)"></ReferencePath>
</
ItemGroup>
</
Target>

This causes the build to pick up secondary references, with the condition forcing Visual Studio to ignore the contents of the ItemGroup tag (and thus do nothing at all.)

In this case the opposite of true is not false; I think it's empty string on the build server. The not equals condition does the trick.

Batch file to back up files recursively

by jim Jan 04, 2013 4:22 PM

We've got a bunch of websites, and I found myself backing up all the (fifteen) web.config files today, in preparation for releasing new versions of everything. Being a software developer, the first thing I think to do when faced with a few minutes of repetitive manual work is to spend far more time automating it.

So with a structure like this:

image

Where I've got web.config files in various subfolders, the following batch file will copy each web.config file to a file named web.config.bak, in the same folder.

@echo off

FOR /r c:\temp\websites\ %%f IN (web.config) DO (
	IF EXIST %%f (
		call :backup_file %%f
	)
)
goto :eof

:backup_file 
(
	copy "%~1" "%~dp1web.config.bak"
	exit /b
)

:eof
pause

Tags:

Code