icanhaz not finding template

[亡魂溺海] 提交于 2019-12-23 03:53:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!