Template two models in one view - Backbone/Marionette

后端 未结 2 1258
迷失自我
迷失自我 2021-02-09 02:08

I\'m trying to use two models in one view, and template using both of them. I\'m working with Marionette. Here is me initialization of the view:

main_app_layout.         


        
相关标签:
2条回答
  • 2021-02-09 02:38

    You can override the serializeData method on your view and have it return both models' data:

    
    APP.Views.HeaderView = Backbone.Marionette.ItemView.extend({
    
      // ...
    
      serializeData: function(){
        return {
          model1: this.model.toJSON(),
          model2: this.options.model2.toJSON()
        };
      }
    
    });
    

    Then your template would look like this:

    <script id="view_template" type="text/template">
        <p>{{model1.label}} {{model1.data}}</p>
        <p>{{model2.label}} {{model2.data}}</p>
    </script>
    
    0 讨论(0)
  • 2021-02-09 02:40

    try creating a complex model that contains the two models, this new model will have the other two as properties and you can acccess the properties of each one like this answer explains..

    Access Nested Backbone Model Attributes from Mustache Template

    0 讨论(0)
提交回复
热议问题