Ladislav M
Ladislav M

Reputation: 2187

How to get model property in className of ItemView?

I my app I need to render ul of users with their ids in class attribute of li. S I tried to do it like this:

Marionette.ItemView.extend({
    tagName: 'li',
    className: this.model.get('user_id'),
        template: userTpl
});

But it didn't work. Is it possible to achieve this some other way?

Upvotes: 4

Views: 1071

Answers (3)

Vic
Vic

Reputation: 8961

can't you do something like this?

className: function(){
  return this.model.get('user_id');
}

Upvotes: 2

Vasiliy vvscode Vanchuk
Vasiliy vvscode Vanchuk

Reputation: 7159

You can put this logic to onBeforeRender method

Marionette.ItemView.extend({
    tagName: 'li',
    template: userTpl,
    onBeforeRender: function(){
          this.className = this.model.get('user_id'),
    }
});

Upvotes: 1

Rida BENHAMMANE
Rida BENHAMMANE

Reputation: 4129

Try to use setElement, here's the documentation :

If you'd like to apply a Backbone view to a different DOM element, use setElement, which will also create the cached $el reference and move the view's delegated events from the old element to the new one.

Upvotes: 0

Related Questions