jQuery templates and dynamic arrays
Hi. I'm using the following jQuery code to render templates
- var units = ${units};
- var unitCommands = ${unitCommands};
- $(document).ready(function() {
- $("#unitListTemplate").tmpl(units).appendTo("#unitList");
- $(units).each(function(index,element) {
- var unitType = element.unitType;
- var provinceName = element.provinceName;
- var filteredUnitCommands = $(unitCommands).filter(function (index) {
- return this.unitType == unitType && this.provinceName == provinceName;
- });
- var selectOrderString = "select[name='" + provinceName + "']"; $(selectOrderString).change(function () {
- var selectedCommand = $(selectOrderString).val();
- var fromLocationSelector = "#" + provinceName + " .fromLocation";
- var toLocationSelector = "#" + provinceName + " .toLocation";
- var properFilterFunction = function (orderCommand) {
- var unitCommandsByOrderType = $(filteredUnitCommands).filter(function (index) { return this.orderType == orderCommand;
- });
- $(fromLocationSelector).empty();
- var q = $.tmpl("<${srcName}>", unitCommandsByOrderType);
- $("#fromLocationTemplate").tmpl(unitCommandsByOrderType).
- appendTo(fromLocationSelector); $(fromLocationSelector).change(function() { $(toLocationSelector).empty();
- var unitCommandsBySrcName = $(unitCommandsByOrderType). filter(function (index) {
- var srcName = this.srcName;
- var value = $(fromLocationSelector).val();
- return this.srcName == $(fromLocationSelector).val();
- }); $("#toLocationTemplate").tmpl(unitCommandsBySrcName).
- appendTo(toLocationSelector);
- });
- };
- properFilterFunction(selectedCommand);
- });
- });
- });
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.