M
M
Max Pavlyukevich2015-02-22 16:34:12
JavaScript
Max Pavlyukevich, 2015-02-22 16:34:12

Using if in a backbone/underscore template, how?

There is Backbone.Viewto change the background of $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()});
                }
            });
        });
    }
});

Accordingly, when I click on, .svs-upload-backgroundI call Popup/Mediathe template with the necessary media:
<% if(this.data.length > 0){ %>
 <div class="MediaList">
    <img/>
 </div>
<% } %>

To change the image, I use a similar Backbone.Viewone that calls the same Popup/Mediatemplate on click on.svs-upload-image
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()});
                }
            });
        });
    }
});

The problem is this: for each popup (image/background) I want to use a different title.
If background:
<% if(' **какой код нужен здесь?** '){%>
   <div class="background_header">Изменения бэкграунда</div>
<% } %>

If image:
<% if(' **какой код нужен здесь?** '){%>
   <div class="image_header">Измнение изображения</div>
<% } %>

what do I need to replace what code is needed here? , thanks a lot!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
aen, 2015-02-23
@aen

The view must work with the model. Add one property in your model that will be responsible for the popup type. In the template, check the value of this property and, according to it, draw what you need.
<% if (type) {} %>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question