使用“;如果“;在Backbone/Undercore模板中,如何

Use "if" in Backbone/Underscore template, how?

本文关键字:如何 如果 Backbone 使用 Undercore      更新时间:2024-03-17

我让Backbone.View更改$el:的背景

var Background = Backbone.View.extend({
    className: 'svs-widget svs-widget-animate',
    events:{
        'click .svs-upload-background': 'media'
    },
    initialize: function(){
    },
    render: function(){
        this.$el.append(_.template(_background).apply(this.options));
        return this.$el;
    },
    media: function(){
        var me = this;   
        require(['View/Popup/Media'], function(_Media){
            $.ajax({
                url: '/svs-ajax.php',
                type: 'post',
                data: {
                    'action':                   'change_bg'
                },
                success: function(data){
                    var media   = new _Media({model: data, popup: popup, target: me.options.background});
                    var popup   = new Popup({content: media.render()});
                }
            });
        });
    }
});

当点击.svs-upload-background时,我正在用media:调用Popup/Media模板

<% if(this.data.length > 0){ %>
 <div class="MediaList">
    <img/>
 </div>
<% } %>

对于更改图像,我有类似的Backbone.View,在.svs-upload-image上调用相同的Popup/Media点击:

var Image = Backbone.View.extend({
    className: 'svs-widget svs-widget-animate',
    events:{
        'click .svs-upload-image':  'media'
    },
    initialize: function(){
    },
    render: function(){
        this.$el.append(_.template(_image).apply(this.options));
        return this.$el;
    },
    media: function(){
        var me = this;   
        require(['View/Popup/Media'], function(_Media){
            $.ajax({
                url: '/svs-ajax.php',
                type: 'post',
                data: {
                    'action':                   'change_image'
                },
                success: function(data){
                    var media   = new _Media({model: data, popup: popup, target: me.options.image});
                    var popup   = new Popup({content: media.render()});
                }
            });
        });
    }
});

问题是:我需要在Popup/Media模板中为背景和图像显示不同的标题,比如:

如果背景:

<% if(' **code needs to be here** '){%>
   <div class="background_header">Changing background</div>
<% } %>

如果图像:

<% if(' **code needs to be here** '){%>
   <div class="image_header">Changing image</div>
<% } %>

我需要粘贴什么而不是"代码需要在这里"?非常感谢大家!

据我所知,您并不是在要求类似这是因为您希望根据代码定制答案,而不是简单地对语法有疑问。

您的问题不是很清楚,但我假设基本问题是,数据模型中没有任何东西可以区分这两种视图,您想知道如何在模板渲染级别区分背景视图和图像视图。你有两个选择:

  1. 让视图将明确标识源类型的变量传递给数据模型,然后在模板中测试该变量
  2. 制作两个不同的模板

我推荐选项1用于DRY目的。我希望这能回答你的问题,但很难给你一个好的答案,因为你的问题不是很清楚。

相关文章: