backbone.js view renders before model fetch

前端 未结 5 981
自闭症患者
自闭症患者 2021-01-05 15:51

I\'m trying to make a small backbone.js app, but struggle with the order of things.

In my html file, I have two script blocks in the header:



        
5条回答
  •  悲&欢浪女
    2021-01-05 16:18

    In this case you should bootstrap your model data so that it's available on page load.

    Instead of

    window.model.fetch();
    

    put something like this in (if using a .erb)

    
    

    Otherwise you need to render the view once the model is fetched e.g.

    bind the view to render when the model changes

    initialize: function () {
        _.bindAll(this, 'render');
    
        this.model.on("change", this.render);
    },
    

    or handle the success of the model.fetch and render the view

    window.model.fetch({
       success: function (model, response) { 
          window.MyApp.myModelView.render();
       }
    });
    

提交回复
热议问题