Setting dynamic css-style properties of a Backbone.View
Asked Answered
S

3

8

I'm trying to set the height, width and background image of a <ul> element.

Here's what I've got for my Backbone.View:

var RackView = Backbone.View.extend({

    tagName: 'ul',

    className: 'rack unselectable',

    template: _.template($('#RackTemplate').html()),

    render: function () {
        this.$el.html(this.template(this.model.toJSON()));
        return this;
    },

    attributes: function () {
        var isFront = this.model.get('isFront');
        var imageUrlIndex = isFront ? 0 : 1;

        return {
            'background-image': 'url(' + this.model.get('imageUrls')[imageUrlIndex] + ')',
            'height': this.model.get('rows') + 'px',
            'width': this.model.get('width') + 'px'
        };
    }
}

This doesn't work because the attributes are not written into a 'style' property of the element. Instead, they're treated like attributes... which makes sense because of the function name, but it has left me wondering -- how do I achieve something like this properly?

The image, height and width are immutable after being set, if that helps simplify...

Do I just wrap my return in a style? That seems overly convoluted...

Here's the generated HTML:

<ul background-image="url(data:image/png;base64,...)" height="840px" width="240px" class="rack unselectable">
</ul>

EDIT: This works, but I'm not stoked:

attributes: function () {

    var isFront = this.model.get('isFront');
    var imageUrlIndex = isFront ? 0 : 1;

    var backgroundImageStyleProperty = "background-image: url(" + this.model.get('imageUrls')[imageUrlIndex] + ");";
    var heightStyleProperty = "height: " + this.model.get('rows') + 'px;';
    var widthStyleProperty = "width: " + this.model.get('width') + 'px;';

    return {
        'style': backgroundImageStyleProperty + ' ' + heightStyleProperty + ' ' + widthStyleProperty
    };
},
Segovia answered 22/8, 2013 at 22:7 Comment(0)
P
6

You could just use the jquery css function in your render function:

render: function () {
    this.$el.html(this.template(this.model.toJSON()));
    var backgroundImageStyleProperty = "background-image: url(" + this.model.get('imageUrls')[imageUrlIndex] + ");";
    var heightStyleProperty = "height: " + this.model.get('rows') + 'px;';
    var widthStyleProperty = "width: " + this.model.get('width') + 'px;';

    this.$el.css({
     'height'          : heightStyleProperty,
     'width'           : widthStyleProperty,
     'background-image': backgroundImageStyleProperty
    });

    return this;
},
Palikar answered 31/3, 2014 at 6:57 Comment(1)
how to select a node with in $el ??Subgroup
C
2

In Marionette, right in your view you can call:

onRender: function () {
  this.$('yourElement').css('whatever', 'css');
},
Cochabamba answered 22/9, 2017 at 13:41 Comment(0)
A
0

The only way to do this is to pass in the attributes using a style attribute as described in the question. In backbone, the attribute map is passed directly into jQuery by this line of code:

var $el = Backbone.$('<' + _.result(this, 'tagName') + '>').attr(attrs);

So, there is no way to pass in styles as a Javascript object.

Anglocatholic answered 12/12, 2014 at 21:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.