Kristian Kristensen’s Blog


Dates With a Sencha Touch Data Model and a WCF JSON Service

Posted in Sencha Touch,WCF by Kristian Kristensen on the June 3rd, 2011

I’ve been developing a Sencha Touch Mobile app recently, that interface with a WCF REST service exposing data via JSON. One of the issues I faced was parsing the dates returned from the service into a date type in the Sencha Touch Data Model. This post explains one way to do that.

Say we have a simple model that only holds an id and a date:

Ext.regModel("MyModel", {
  fields : [
    'id',
    {name : 'MyDate', type : 'date'},
  ]
});

When data is read into this model using a Proxy from a WCF REST JSON service, the MyDate column will just hold the string representation of the data such as “/Date(1238606590509+0800)/”, ie. it won’t be parsed. The trick to overcome this is to utilize the “convert” property that you can set on a field. This allows you to call out to a helper function every time a value will be set on the model for that field.
Now our model looks like this:

Ext.regModel("MyModel", {
  fields : [
    'id',
    {name : 'MyDate', type : 'date', convert : toDateFromJSON},
  ]
});

We need to define our converter function;

function toDateFromJSON(v, record) {
  return Date.parseDate(v, "M$");
}

In this converter function we utilize the built in Date object. When giving it the format string “M$” it will parse Microsoft Serialized AJAX dates which is what’s used with WCF, REST and the JSON formatter OOB.

Now our model will have the correct datatype for the MyDate property, which will allow us to call useful functions on it such as format.

  • If you like my writing you should subscribe to my RSS feed.

    Sencha Touch: Each picker slot is required to have a name

    Posted in PhoneGap,Sencha Touch by Kristian Kristensen on the May 31st, 2011

    I was using a select field in Sencha Touch. When running in Chrome it worked fine, and I could open it and select a value. Deploying it via PhoneGap to an Android phone showed that it didn’t work. Instead I got this error in the “adb logcat” output:

    D/PhoneGapLog( 2323): file:///android_asset/www/lib/sencha/sencha-touch-debug.js
    : Line 25587 : Error: Each picker slot is required to have a name.
    E/Web Console( 2323): Error: Each picker slot is required to have a name. at fil
    e:///android_asset/www/lib/sencha/sencha-touch-debug.js:25587

    Obviously the error message hold the key: namely that the select field needs to have a name. Giving it one, makes the error go away. Still interesting that it works OOB in Chrome. I guess Chrome auto-generates a name for the field and the Android browser doesn’t.

  • If you like my writing you should subscribe to my RSS feed.

    Installing Azure Tools for Visual Studio 2010 Fails On IIS CGI Dependency

    Posted in Azure,Code,Microsoft by Kristian Kristensen on the April 5th, 2011

    I was trying to install the Azure Tools for Visual Studio 2010 via the Web Platform Installer, but it kept on failing on me. I complained on Twitter and @wadewegner helped out.
    Error message I got from the installer (TwitPic)

    So to make the error message I received a bit more google-able I’m writing this blog post with the error I received and how I got it to work.

    Problem was with the CGI feature of IIS. It failed on installation. I got an obscure error line and code in my Web Platform Installer Log:

    Error: 0 : Unattended setup returned ‘-2146498529′

    Googling that and “-2146498529 IIS CGI” didn’t help me. I tried installing just the IIS: CGI dependency via WPI, but that didn’t help. In the end the solution for me was to install CGI for IIS using “Programs and Features” and Windows Components. Then start WPI with the Azure Tools. That got it all working.

    Edited April 19th: Link to image of observed error

  • If you like my writing you should subscribe to my RSS feed.

    Installing Clojure, SLIME in Emacs Via ELPA on Windows With a Bit on Swank

    Posted in Clojure,Code by Kristian Kristensen on the March 28th, 2011

    I’ve been trying to setup Emacs for Clojure development. There’s a bunch of information about this online, but I couldn’t get it to work. It seems most of it is outdated between Emacs, ELPA, SLIME and Swank. So I thought it post the exact steps I took and the links I used to get it to work.
    I’m using Emacs 23 on Windows 7 64-bit.

    • Install ELPA, copy the snippet found at the link into the *scratch* buffer and eval it (M-x eval-buffer).
    • Open package.el which is downloaded during the ELPA isntall. It’s probably located in “~/.emacs.d/elpa/”. Delete the contents of it and replace it with what’s found here. Then re-evaluate the buffer.
    • Add the Marmalade Repository to your .emacs file by including:

      (add-to-list ‘package-archives
      ‘("marmalade" . "http://marmalade-repo.org/packages/") t)

    • Close and restart Emacs
    • Hit M-x package-list-packages
    • It should show you the list of packages available. Find “slime-repl”, and hit “i” to include it. Hit “x” to download and install all selected packages. You want to repeat this process until the following packages are installed:
      • slime-repl
      • clojure-mode
      • paredit
      • slime
    • Restart Emacs, just in case.

    Now you should have the updated packages for running Clojure in Emacs.

    The problem seems to be that the repo and package.el found at the original ELPA site is not longer being maintained. ELPA is being merged into Emacs 24 and so it’s in a bit of an in between state. The package.el that’s being downloaded from github is a copy of the one found in the Emacs 24 repo. I’ve also tried that file directly, but it failed on my Emacs 23 installation. Also the method for running a Clojure REPL inside of Emacs using Swank-clojure seems to be outdated. That tripped me up as well.

  • If you like my writing you should subscribe to my RSS feed.

    Using a Column Alias in a Firebird Where Clause

    Posted in Code,Misc by Kristian Kristensen on the March 24th, 2011

    Here’s an odd thing that tripped me up a bit as I was writing a piece of SQL for Firebird (my ignorance even had me blabbering out on Twitter). I wanted to do some manipulation on a column in a SELECT and then filter on this computed column. I thought I could just give the column an alias, and then use this in the WHERE clause. Something like this:

    SELECT substring(p.Name from 5 for 2) as mycol, p.Name FROM "People" p WHERE mycol = 'Kr'
    

    This doesn’t work though. Parsing fails saying that the column ‘mycol’ doesn’t exist or isn’t known. What you can do is to repeat the computed column like this:

    SELECT substring(p.Name from 5 for 2) as mycol, p.Name FROM "People" p WHERE substring(p.Name from 5 for 2) = 'Kr'
    

    Interestingly enough the column alias does work in the ORDER clause of a SELECT:

    SELECT substring(p.Name from 5 for 2) as mycol, p.Name FROM "People" p ORDER BY mycol
    

    Asking my friend Jakob Andersen he says it has to do with the order in which the expressions are parsed. Makes sense. This also explains why the same behavior occurs if you try to create the same expressions in SQL Server. The other option for solving the above in Firebird is to use an inline view.

  • If you like my writing you should subscribe to my RSS feed.

    Creating a Multi Column Index using Fluent NHibernate

    Posted in Code,FluentNHibernate by Kristian Kristensen on the March 20th, 2011

    Using Fluent NHibernate I wanted to specify my indexes in my auto mapping code. That way I’d have them when using Schema Update, etc. from Fluent NHibernate/NHibernate. Doing that is a matter of using the Index method on your mapping objects, and correlating the columns you want to include in the index using the index name.

     var model = new AutoPersistenceModel();
     model.AddEntityAssembly(Assembly.GetExecutingAssembly())
        .Where(t => t.Namespace.EndsWith("Entities"));
    
    model.Override<MyPoco>(map =>
    {
     map.Map(x => x.ColumnOne).Index("IX_MyColumns");
     map.Map(x => x.ColumnTwo).Index("IX_MyColumns");
    });
    

    When generating the schema it’ll include the SQL for creating an index named “IX_MyColumns” over ColumnOne and ColumnTwo.

  • If you like my writing you should subscribe to my RSS feed.

    Comparing Two Nullable Values In a Workflow Using Visual Basic

    Posted in Code,Misc,WF4 by Kristian Kristensen on the March 17th, 2011

    I needed to compare two Nullable date values in a workflow service. The way it was organized was as a parameter to a method call (using the InvokeMethod Activity), namely the Sort call on a collection of POCOs. The parameter is of type Comparison with T being MyPOCO, and it’s value was something that had to compare two Nullable dates.

    At first the dates couldn’t be null, and hence there wasn’t a problem. I’d just use CompareTo:

    Function(d1, d2) d1.MyDate.CompareTo(d2.MyDate)
    

    Once the values are Nullable though it’s a different game. I had an exception because now values in my collection could have null on the MyDate property.

    System.InvalidOperationException: Failed to compare two elements in the array.
    —> System.InvalidOperationException: Nullable object must have a value.

    My solution was to rewrite the function delegate I pass in, and use Nullable.Compare:

    Function(d1, d2) Nullable.Compare(d1.MyDate, d2.MyDate)
    
  • If you like my writing you should subscribe to my RSS feed.

    X.509 Certificates and Downloads Via Internet Explorer

    Posted in Code,WCF by Kristian Kristensen on the March 14th, 2011

    Re-installing my computer I moved some X.509 certificates and suddenly experienced some very weird behavior. I got the error:

    The incoming message was signed with a token which was different from what used to encrypt the body. This was not expected.

    from WCF, when I tried to call an X.509 protected service. This had all worked before, my binding in WCF hadn’t changed, neither had the server side. One thing was strange though, the thumbprint of the X.509 certificated had changed. It wasn’t a problem loading it into Certificate Manager in MMC, even when importing a certificate that had a password.
    Turns out that during my move of the file Windows Explorer had labeled the source from where I moved it as belonging to the Internet Zone. This means that the file is blocked. Much the same way as when you download a zip file via Internet Explorer, unzip it and nothing works. You have to right-click the file and select “Unblock” for it to unzip properly. After deleting my certificated, unblocking them, and then re-importing everything worked like a charm again.

    There’s a very cool utility from NirSoft called AlternateStreamView that can look at a file or directory and display any alternate streams on that file. The alternate stream is where Windows adds the download source (zone info). This tool will let you scan a directory and remove all alternate streams on files. I found it very useful in removing the errornous zone info that had been added to my files as I’d copied them over.

  • If you like my writing you should subscribe to my RSS feed.

    Using an external log4net config file

    Posted in Code by Kristian Kristensen on the March 7th, 2011

    I’ve been using log4net in a console app and wanted to separate the log4net configuration from the rest of the applications configuration. Mainly to make it easier to deal with, but also to be able to easily update the log conf file separately during deployment. I thought I’d use the standard configSource approach to separate configuration sections to individual files, but alas that doesn’t work. Instead you need to add the regular log4net section name and add a “secret” application setting to accomplish this. Easy enough to do when you know it.

    So:

    <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
      </configSections>
    

    and:

    <appSettings>
        <add key="log4net.Config" value="log4net.config" />
    </appSettings>
    
  • If you like my writing you should subscribe to my RSS feed.

    Using the same mapping for Firebird and Sql Server from Fluent NHibernate

    Posted in Code,FluentNHibernate by Kristian Kristensen on the March 4th, 2011

    In a recent project I’ve been using Fluent NHibernate to generate my NHibernate mappings automatically from my POCO’s. Another requirement in this project is that when deployed the database server should run Firebird. I’ve been using Microsoft Sql Server locally, and really wanted to be able to switch seamlessly between the two databases. Primarily if it turned out later that Sql Server would be the way to go instead of Firebird.
    When switching to Firebird my mappings didn’t work. The reason is that Firebird uses generators for auto-incrementing columns. Using auto-mapping and an auto-incrementing column that’ll work between Firebird and Sql Server is a matter of writing this in your auto mapping overrides:

    model.Override<MyPoco>(map =>  {
      map.Id(x => x.Id).GeneratedBy.Native("MyPoco_Gen");
    }
    

    When the schema is generated from Fluent NHibernate it’ll work with SQL Server and Firebird.

    This post on Stackoverflow describes how to do it with XML and regular NHiberante mappings.

  • If you like my writing you should subscribe to my RSS feed.

    « Previous PageNext Page »