繁体   English   中英

_.template不起作用-bone.js

[英]_.template not working - backbone.js

尝试使用Backbone.js通过模板从“视图”中输出一些值,但是列表项未显示在网页上,请查看一下并告知丢失的内容。 谢谢

<script type="text/template" id="myid">
    <li>  <%= value %> </li>
</script>
<script type="text/javascript">
(function($){
    ListView = Backbone.View.extend({
        tagName:'ul',
        initialize:function(){
            this.template = _.template($('#myid').html());
        },
        render:function(){
        this.$el.append(this.template({value:"Joe Blogg"}));
        return this;    
        }
    });
    $(document).ready(function(e) {
        myListView = new ListView();
    });
  })(jQuery);

请尝试这个。

(function($){
    var ListView = Backbone.View.extend({
        tagName:'ul',
        $el: $(this.tagName),
        initialize:function(){
            this.template = _.template($('#myid').html());
        },
        render:function(){
            this.$el.append(this.template({value:"Joe Blogg"}));
            $('body').append(this.$el);
        }
    });
    $(document).ready(function(e) {
        var myListView = new ListView(); // call initialize()
        myListView.render(); // call render()
    });
})(jQuery);

我使用jQuery.js,Underscore.js和Backbone.js。

并始终使用“ var”。

尝试这个

   this.$el.empty();
   this.$el.html(this.template(this.model.attributes));

DOM ready事件中,调用.render()方法

$(document).ready(function(e) {
     myListView = new ListView();
     myListView.render(); 
 });

您需要调用render ,然后将视图的el实际添加到页面的某处。

$(document).ready(function(e) {
   var myListView = new ListView();
   $("body").append(myListView.render().el); 
 });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM