1

尝试使用 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);
4

3 回答 3

4

请试试这个。

(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”。

于 2013-01-25T02:10:07.263 回答
1

试试这个

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

在您的DOM 就绪事件中调用该.render()方法

$(document).ready(function(e) {
     myListView = new ListView();
     myListView.render(); 
 });
于 2013-01-25T01:43:50.280 回答
0

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

$(document).ready(function(e) {
   var myListView = new ListView();
   $("body").append(myListView.render().el); 
 });
于 2013-01-25T01:43:24.477 回答