jQuery templates and dynamic arrays

jQuery templates and dynamic arrays

Hi.  I'm using the following jQuery code to render templates

  1. var units = ${units}; 
  2.  var unitCommands = ${unitCommands}; 
  3.  $(document).ready(function() { 
  4.       $("#unitListTemplate").tmpl(units).appendTo("#unitList"); 
  5.       $(units).each(function(index,element) { 
  6.              var unitType = element.unitType; 
  7.              var provinceName = element.provinceName; 
  8.              var filteredUnitCommands = $(unitCommands).filter(function (index) { 
  9.                    return this.unitType == unitType && this.provinceName == provinceName; 
  10.              });
  11.              var selectOrderString = "select[name='" + provinceName + "']";             $(selectOrderString).change(function () { 
  12.                    var selectedCommand = $(selectOrderString).val(); 
  13.                    var fromLocationSelector = "#" + provinceName + " .fromLocation"; 
  14.                    var toLocationSelector = "#" + provinceName + " .toLocation"; 
  15.                    var properFilterFunction = function (orderCommand) { 
  16.                          var unitCommandsByOrderType = $(filteredUnitCommands).filter(function (index)                               { return this.orderType == orderCommand; 
  17.                                }); 
  18.                    $(fromLocationSelector).empty(); 
  19.                     var q = $.tmpl("<${srcName}>", unitCommandsByOrderType); 
  20.                                                                                               $("#fromLocationTemplate").tmpl(unitCommandsByOrderType).
  21.                         appendTo(fromLocationSelector); $(fromLocationSelector).change(function() {                               $(toLocationSelector).empty(); 
  22.                                var unitCommandsBySrcName = $(unitCommandsByOrderType).                                     filter(function (index) { 
  23.                                            var srcName = this.srcName; 
  24.                                            var value = $(fromLocationSelector).val(); 
  25.                                            return this.srcName == $(fromLocationSelector).val(); 
  26.                                      });                                                                                       $("#toLocationTemplate").tmpl(unitCommandsBySrcName).
  27.                                     appendTo(toLocationSelector); 
  28.                          }); 
  29.                    }; 
  30.                    properFilterFunction(selectedCommand); 
  31.              }); 
  32.        }); 
  33.  });


I'm using the Play web framework written in Scala and it also uses the ${} symbols for code insertion.  So I sometimes have to escape the $ symbol in the javascript code.   But anyways, what I'm having issues with that I can't understand is why the template call in var q = $.tmpl(   isn't returning a list of compiled templates.  The data exists, when I place a debug point in Firebug, but it ends up returning a div element.   The code works only on the first call to the template, and the only thing that I can think of is that the data on the top is static to the web page (server side Play template) rather than being generated dynamically.  So the template
"#unitListTemplate" generates fine but "#toLocationTemplate" and "#fromLocationTemplate" do not.