How to prevent Backbone.Marionette from rendering a view if it's model hasn't been fetched?

前端 未结 4 655
暗喜
暗喜 2020-12-31 10:08

In my backbone.Marionette application I have a Model that requires an Id attribute to construct it\'s url. I therefore create the model by passing it an Id, add it to a view

4条回答
  •  难免孤独
    2020-12-31 10:43

    Both answers above work.
    Here's yet another way which I prefer to use (feels more backboney).

    Bind the view directly to the model
    When the model is done being fetched, the function you specify will run. This can be done with models or collections.

    var Model = Backbone.Model.extend({});
    var model = new Model();
    model.fetch();
    
    var View = Marionette.ItemView.extend({
        model:model,
        initialize: function(){
            this.model.bind("sync", this.render, this);// this.render can be replaced
        }                                              // with your own function
    });                               
    
    app.region.show(new View);                                           
    

    This also enables you to fetch whenever you want.

提交回复
热议问题