JST与骨干网和下划线 [英] JST with Backbone and Underscore

查看:239
本文介绍了JST与骨干网和下划线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的骨干,并强调建立一个小型试验基地。

I am using Backbone and Underscore to create a small test site.

我编译我所有的HTML模板文件合并成一个JST JavaScript文件的建议<一href=\"http://stackoverflow.com/questions/8366733/external-template-in-underscore#answer-8377369\">here和<一个href=\"http://stackoverflow.com/questions/8366733/external-template-in-underscore#answer-14337492\">here.

I am compiling all my html template files into one JST javascript file as suggested here and here.

这不是然而,很明显如何使用模板文件使用此。我曾尝试这样的:

It isn't however very obvious how to use this with template files. I have tried this:

App.HeaderView = Backbone.View.extend({
    el: '#headerContent',
    template: JST['header.html'](),
    //template: window["JST"]["header.html"],
    //template: _.template("<h1>Some text</h1>"),

    initialize: function () {
        this.render();
    },

    render: function() {
        //var html = this.template();
        //// Append the result to the view's element.
        //$(this.el).append(html);
        this.$el.html(this.template());
        return this; // enable chained calls
    }
});

我得到的错误是JST.header.html是不是一个函数。

The error I get is JST.header.html is not a function.

(最后一个注释掉位作品被方式模板:_.template(&LT; H1&GT;有的文字&lt; / H1&gt;中),所以我知道这个问题是不与任何其他)。

(The final commented out bit works by the way template: _.template("<h1>Some text</h1>") so I know the problem isn't with anything else).

这可能是因为我使用browserify(所以曾尝试'需要'的文件),但我已经试过的'包括'模板文件,包括直接添加几种不同的方式:

It might be because I am using browserify (so have tried 'requiring' the file), but I have tried several different ways of 'including' the template file, including adding it directly:

<script src="templates/_templates.js"></script>
<script src="js/functions.js"></script>
</body>
</html>

需要做什么任何想法来得到这个工作?

Any ideas what needs to be done to get this to work?

推荐答案

在3行,你不想调用模板,而只需使用:结果
模板:JST ['了header.html']

On line 3 you don't want to invoke the template, rather just use:
template: JST['header.html'].

目前你设置模板等于函数的返回值,然后试图调用的返回值而不是实际的功能,因此它被养是不是一个函数的错误。

Currently you're setting template to equal the return value of the function and then trying to invoke that return value instead of the actual function, so it is raising a "is not a function" error.

这篇关于JST与骨干网和下划线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆