How to synchronize multiple Backbone.js fetches?

前端 未结 3 2158
抹茶落季
抹茶落季 2020-12-23 21:28

I’m a bit new to Backbone.js, but am already impressed by everything it can do for me, and am trying to learn the patterns and best practices now.

I have two collect

3条回答
  •  死守一世寂寞
    2020-12-23 22:28

    I like @JayC's answer if you have to fetch each collection individually. You could argue that a single fetch to the server would be better as opposed to multiple fetches. If you are fetching this data on application load, I would do a single call to the server and then pass the data to your relevant collections. It truly depends on how many collections you have to fetch on application load, but I honestly prefer making one call to the server then passing the data to my relevant collections.

    $.ajax({
        url: "path/to/app-load-data"
    }).done(function(data) { 
        collA = new CollA(data.collA);
        collB = new CollB(data.collB);
    });
    

    This obviously will depend if you can manipulate your api and the above does not follow REST. But then again you are making one call to the server as opposed to multiple calls.

提交回复
热议问题