icanhaz not finding template
Asked Answered
H

1

1

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?

Hersey answered 25/7, 2012 at 4:35 Comment(0)
O
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.

Oospore answered 25/7, 2012 at 4:49 Comment(2)
Thanks Andrew for your quick reply. I simply figured that that browser would download the script synchronously and that the contents of src would be available in the DOM by the time I used anything passed into $(document).ready. Isn't that typically what happens with text/javascript script tags?Hersey
It is for Javascript, but the contents of the script are never actually injected into the element itself (ie. it's innerHTML), which is what ICH checks for.Oospore

© 2022 - 2024 — McMap. All rights reserved.