service.template_base_path = 'share/print_templates/t_';
service.server_template_path = '/print_template';
+ // Cache of template names that are known to be generated from
+ // traditional TT2 files instead of being server-manged.
+ service.localTemplates = {};
+
/*
* context : 'default', 'receipt','label', etc.
* scope : data loaded into the template environment
content: xhttp.responseText,
contentType: this.getResponseHeader('content-type')
});
+
+ } else if (this.status === 404) {
+ console.debug('Template ' + templateName + ' is not '
+ + 'hosted on the server; using local template');
+ reject();
+
} else {
+ // other error
reject('Error compiling print template');
}
}
});
}
+
+ service.getTt2PrintTemplate = function(name) {
+ var path = service.template_base_path + name;
+ console.debug('fetching TT2 print template: ' + path);
+
+ return $http.get(path).then(
+ function(data) {
+ console.debug('Found server template file for ' + name);
+ service.localTemplates[name] = true;
+ return data.data;
+ },
+ function() {
+ console.error('unable to locate print template: ' + name);
+ return $q.reject();
+ }
+ );
+ }
+
// loads an HTML print template by name from the server If no
// template is available in local/hatch storage, fetch the template
// as an HTML file from the server. if no HTML file is available,
return;
}
- var path = service.template_base_path + name;
- console.debug('fetching template ' + path);
-
- $http.get(path).then(
- function(data) {
-
- if (data.data.match(/Print Template Not Found/)) {
-
- // AngJS templates return a dummy template w/ the
- // above text if the template is not found instead
- // of a 404.
- return service.compileRemoteTemplate(name, args.scope)
- .then(
- function(response) {
- console.debug('Found server-hosted template for ' + name);
- args.content_type = response.contentType;
- args.content = response.content;
- deferred.resolve(args.content);
- },
- function() {
- console.error('unable to locate print template: ' + name);
- deferred.reject();
- }
- );
- }
+ if (service.localTemplates[name]) {
+ service.getTt2PrintTemplate(name)
+ .then(deferred.resolve, deferred.reject);
+ return;
+ }
- console.debug('Found server template file for ' + name);
- deferred.resolve(data.data)
+ // Template may be TT2 or server-managed.
+ // Try server-managed first, then fall back to TT2.
+ return service.compileRemoteTemplate(name, args.scope)
+ .then(
+ function(response) {
+ console.debug('Found server-hosted template for ' + name);
+ args.content_type = response.contentType;
+ args.content = response.content;
+ deferred.resolve(args.content);
},
function() {
- console.error('unable to locate print template: ' + name);
- deferred.reject();
+ console.debug('Template ' + name + ' is not server-managed');
+ service.getTt2PrintTemplate(name)
+ .then(deferred.resolve, deferred.reject);
}
);
});