简体   繁体   中英

Uncaught TypeError with Handlebars.js

I am using Handlebars.js with precompiled templates. I have the following code (using JQuery in the first line):

$('#'+id).append('<script src="'+widgetContext.templateDir+
                                             template+'.tmpl">');
console.log(Handlebars);
console.log(Handlebars.templates);                                       
var html = Handlebars.templates[template](data);

I get the following at the console:

在此处输入图片说明

The 'widget_container' template file definitely exists in the location specified. I've also tried with both the standard handelbars.js and handelbars.runtime.js.

Any idea what is going on here or how to fix the error?

Update: it appears to work if the HTML is on the same domain as the javascript file and templates but not if it isn't, so something to do with the same origin policy? I'm trying the handlebars require.js plugin here https://github.com/SlexAxton/require-handlebars-plugin but having problems getting it working.

I solved this using:

  • The text plugin for require.js and loading the handlebars templates as text files
  • The optimizeAllPluginResources build option for r.js
  • Getting a very recently updated version of require.js/r.js which fixed a bug with the optimizeAllPluginResources build option
  • A rather horrible use of eval to run the template file code - would still be nice to avoid this

Still not quite sure exactly what was going on with the undefined Handlebars.templates though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM