【问题标题】:Switch between two html templates based on condition in backbone js views根据主干js视图中的条件在两个html模板之间切换
【发布时间】:2018-07-07 17:23:29
【问题描述】:

我是主干 js 的新手...我有一个应用程序,我需要在主干视图中的两个不同 html 模板之间切换。我应该如何在单个视图中定义这些模板并根据我的条件在该模板上渲染函数。

【问题讨论】:

    标签: html templates laravel-5 backbone.js view


    【解决方案1】:

    您可以通过以下方式渲染两个不同的模板

                                        <div>
                                            <%if(condition){%>>
                                                  <div>Template 1</div>
                                            <%}else{%>
                                                 Template 2
                                                    <%}%>
                                        </div>
    

    【讨论】:

      【解决方案2】:
      var PersonView = Backbone.View.extend({
          tagName: 'li',
      
          template_1: _.template("<strong><%= name_1 %></strong> (<%= age_1 %>) - <%= occupation_1 %>"),
          template_2: _.template("<strong><%= name_2 %></strong> (<%= age_2 %>) - <%= occupation_2 %>"),
          initialize: function(){
              this.render();
          },
      
          render: function(){
              if (condition) {
                this.$el.html( this.template_1(this.model.toJSON()));
              } else {
                  this.$el.html( this.template_2(this.model.toJSON()));
              }
          }
      });
      

      【讨论】:

      • @anu-prabha 请检查
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多