下划线js模板中的referenceerror

时间:2012-02-08 06:55:08

标签: node.js backbone.js underscore.js

我正在使用带有节点js的backbonejs尝试将变量从骨干视图传递到模板(html文件)。渲染视图模板的功能如下:

render: function(event){
        var compiled_template = _.template( $("#results-template").html() );
        console.log(myPhoto.toJSON());
        $(this.el).html(compiled_template(myPhoto.toJSON()));    
        return this;
    }

myPhoto有一个值:

{
        src: 'placeholder.jpg',
        title: 'an image placeholder',
        coordinates: [0,0],
        tags:  ['untagged'],
        location: 'home'
 }

并在html文件中:

<script id="results-template" type="text/template">               
        <h2><%= title %></h2>
        <p>testing....</p>
    </script>

当我执行它时会出现类似

的错误
ReferenceError: c:\trello\testApp/views/test.html:19
    17|     <script id="results-template" type="text/template">       
    18|        
 >> 19|         <h2><%= title %></h2>        
    20|         <p>testing....</p>
    21|     </script>
    22|     

title is not defined
    at Object.<anonymous> (eval at <anonymous> (c:\trello\node_modules\ejs\lib\ejs.js:203:1))
    at Object.<anonymous> (c:\trello\node_modules\ejs\lib\ejs.js:201:15)
    at ServerResponse._render (c:\trello\node_modules\express\lib\view.js:425:21)
    at ServerResponse.render (c:\trello\node_modules\express\lib\view.js:318:17)
    at c:\trello\testApp\test.js:23:16
    at callbacks (c:\trello\node_modules\express\lib\router\index.js:272:11)
    at param (c:\trello\node_modules\express\lib\router\index.js:246:11)
    at pass (c:\trello\node_modules\express\lib\router\index.js:253:5)
    at Router._dispatch (c:\trello\node_modules\express\lib\router\index.js:280:4)
    at Object.handle (c:\trello\node_modules\express\lib\router\index.js:45:10)

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

你确定你的所有照片都有标题吗?空属性可能导致此错误。您可以通过在主干Model类中添加默认值(例如空字符串)来解决此问题。