问题
To make this example as simple as possible, let's say I have the following code in home.html:
<html>
<head>
<!-- ALL DEPENDENCIES FOR ICANHAZ ARE INCLUDED ABOVE -->
<script type="text/html" id="foo" src="js_template.js"></script>
<script>ich.foo({})</script>
</head>
<body></body>
</html>
And in javascript_template.js, I have the following:
Hello world!
As it turns out, icanhaz is not detecting foo, so ich.foo({}) is throwing an error. What exactly is going on here?
回答1:
ICanHaz.js does not automatically download the contents of src
. This behavior can be seen on line 510 of ICH.js's source code, in which it checks for an innerHTML property of the script tag before defining the template.
You must define it inline, or use your own AJAX request. For example, embedded:
<script type="text/html" id="foo">
Hello, world
</script>
Or, if you are using jQuery, you can use AJAX to load the script:
$(function(){
$.get('js_template.js', function(res){
ich.addTemplate('foo', res);
});
});
Keep in mind, ich.foo()
will not be available until the AJAX request completes.
来源:https://stackoverflow.com/questions/11642794/icanhaz-not-finding-template