Backbone.js + Rest. Collection is not populated after fetch()

前端 未结 1 2055
陌清茗
陌清茗 2020-12-04 11:00

I\'m new in Backbone. So I\'m trying to fetch data from REST service.

this is my simple code:

$(function () {

    var Entity = Backbone.Model.extend         


        
相关标签:
1条回答
  • 2020-12-04 11:24

    I think you are on the right way. But because Backbone.Collection.fetch() is async, you should check value of entityList.models not right after the method call, but in success callback of fetch.

    That is, this code will say that models list is empty:

    entityList.fetch();
    console.log(entityList.models); // => 0 (collection being fetched)
    

    while this code will print number of models in the collection when it have been populated:

    entityList.fetch({success: function(){
        console.log(entityList.models); // => 2 (collection have been populated)
    }});
    
    0 讨论(0)
提交回复
热议问题