2013-03-28 2 views
0

alert()render의 내부에서 사용하면 UserListView의 출력이 올바르게 표시됩니다. 내가 alert()라고 말하면,보기는 표시되지 않습니다.보기가 올바르게 표시되지 않습니다.

저는 여기에 붙어 있습니다. 왜 그런가요?

var User = Backbone.Model.extend({ 
    defaults: {} 
}); 

var UserCollection = Backbone.Collection.extend({ 
    model: User, 
    url: 'http://localhost/cirest/index.php/api/userapi/users' 
}); 

var UserView = Backbone.View.extend({ 
    tagName: 'thumbnail', 
    className: 'span12', 
    template: $('#userdetailtemplate').html(), 
    render: function() { 
     var tmpl = _.template(this.template); 
     this.$el.html(tmpl(this.model.toJSON())); 
     return this; 
    } 
}); 

var UserListView = Backbone.View.extend({ 
    el: ('#content1'), 

    initialize: function() {   
     this.collection = new UserCollection(); 
     this.collection.fetch(); 

     this.render(); 
    }, 

    render: function() { 
     var that = this; 

     alert('asdf'); 

     _.each(this.collection.models, function (item) { 
      that.renderUser(item); 
     }, this); 

    }, 

    renderUser: function (item) { 
     var userview = new UserView({ 
      model: item 
     }); 

     this.$el.append(userview.render().el); 
    } 
}); 

$(function() { 
    var uview = new UserListView(); 
}); 

html로 페이지 : : 여기

코드입니다

<div id="content1" class="span12"></div> 
<script type="text/template" id="userdetailtemplate"> 
    <%= user_name %> <%= user_email %> 
</script> 
+0

이와 비슷한 것 http://stackoverflow.com/questions/8413500/backbone-js-populating-a-collection/8415515#8415515? – nikoshr

+0

안녕하세요 @nikoshr 의견을 보내 주셔서 고맙습니다. –

답변

0
I가 또한 @nikoshr에 대한

initialize: function() { 
     this.collection = new UserCollection(); 
     this.collection.fetch(); 
     this.collection.on("reset", this.render, this); 
     this.render(); 
    }, 

으로 감사를 UserListView의 초기화 기능을 변경하여 작업있어

해당 링크를 참조하십시오

관련 문제