使用带有 Backbone 的 Handlebars [英] Using Handlebars with Backbone

查看:30
本文介绍了使用带有 Backbone 的 Handlebars的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Backbone/Handlebars/Require.我已经在网上和 SO 上查看了所有内容 - 是否有任何教程或网站可以指导我访问这些教程或网站,以提供有关使用把手而不是下划线的有用信息?

I am learning Backbone/Handlebars/Require. I have looked all over online and on SO - are there any tutorials or websites that you can direct me to that would provide helpful information for using using handlebars instead of underscore?

推荐答案

使用 handlebars.js 而不是 underscore 模板非常简单.看看这个例子:

Using handlebars.js instead of underscore templating is pretty straightforward. Check out this example:

https://cdnjs.com/libraries/backbone.js/tutorials/what-is-a-view(滚动到加载模板"部分)

https://cdnjs.com/libraries/backbone.js/tutorials/what-is-a-view (scroll to the "Loading a Template" section)

SearchView = Backbone.View.extend({
    initialize: function(){
        this.render();
    },
    render: function(){
        // Compile the template using underscore
        var template = _.template( $("#search_template").html(), {} );
        // Load the compiled HTML into the Backbone "el"
        this.el.html( template );
    }
});

基本上,主干中的约定是在渲染函数中构建您的 html.模板引擎的使用完全取决于您(我喜欢 Backbone).因此,您只需将其更改为:

Basically, the convention in backbone is to build your html in a render function. The use of templating engine is left completely up to you (which I like about Backbone). So you'd just change it to:

SearchView = Backbone.View.extend({
    initialize: function(){
        this.render();
    },
    render: function(){
        // Compile the template using Handlebars
        var template = Handlebars.compile( $("#search_template").html() );
        // Load the compiled HTML into the Backbone "el"
        this.el.html( template );
    }
});

由于您使用的是 require.js,您可以将 Handlebars 设为模块顶部的依赖项.我对此很陌生,但听起来学习重点是 Backbone.js 模式和 require.js 用法.

Since you're using require.js, you can make Handlebars a dependency at the top of your module. I'm pretty new to this, but it sounds like the learning to focus on would be Backbone.js patterns and require.js usage.

这篇关于使用带有 Backbone 的 Handlebars的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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