【问题标题】:Backbone js.How to pass data from view to template in backbone js骨干js.如何在骨干js中将数据从视图传递到模板
【发布时间】:2014-06-29 15:14:56
【问题描述】:

我是 Backbone js 的新手。有人可以帮我从我的视图中发送模板中的数据吗?

我的视图有这个代码:

$('#top-bar').html(_.template($("#loginned-top-bar-template").html()));

我的模板包含

<li class="menu-item"><a href="javascript:void(0)" id="topbar-username"><%user_name%></a></li>

我想向它发送“awsome_user”。

如果有人能帮助我,那就太好了。

【问题讨论】:

    标签: javascript jquery html templates backbone.js


    【解决方案1】:
    var compiled = _.template($("#loginned-top-bar-template").html());
    var templateVars = {user_name : 'awesome_user' };
    $('#top-bar').html( compiled(templateVars) );
    

    如果要打印变量,&lt;%user_name%&gt; 应该是 &lt;%=user_name%&gt;

    如果您想使用其他user_name,请在调用编译函数之前设置user_name属性。

    var compiled = _.template($("#loginned-top-bar-template").html());
    var templateVars = {user_name : 'awesome_user' };
    templateVars.user_name = Parse.User.current().get("name");
    $('#top-bar').html( compiled(templateVars) );
    

    【讨论】:

    • 如果我想用我的 Parse 对象的属性替换“awesome_user”怎么办。这将是-- Parse.User.current().get("name") --
    猜你喜欢
    • 1970-01-01
    • 2015-05-23
    • 2013-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-17
    • 2012-12-06
    • 2012-02-26
    相关资源
    最近更新 更多