Мне интересно, если это лучшая практика или если мне не хватает простого способа динамического изменения класса элемента:
define(function () {
'use strict';
var NextButtonView = Backbone.View.extend({
className: 'disabled halfGradient',
template: _.template($('#nextButtonTemplate').html()),
attributes: function() {
return {
id: 'NextButton',
title: chrome.i18n.getMessage("skipNextVideo")
};
},
render: function () {
this.$el.html(this.template(this.model.toJSON()));
this.$el.toggleClass('disabled', !this.model.get('enabled'));
return this;
},
initialize: function () {
this.listenTo(this.model, 'change:enabled', this.render);
}
});
return NextButtonView;
});
где мой шаблон:
<script type="text/template" id="nextButtonTemplate">
<svg width="16px" height="16px">
<rect class="path" x="14" y="3" width="2" height="10" fill="gray" />
<path class="path" d="M0,3 L7,8 L0,13z" fill="gray" />
<path class="path" d="M7,3 L14,8, L7,13z" fill="gray" />
</svg>
</script>
Существует ли более "неявный" способ установки класса на основе состояния модели?
Я не вижу ничего плохого в том, что у вас есть, но вы также можете сделать следующее:
className: function() {
return 'halfGradient' + ( this.model.get('enabled') ? '' : ' disabled');
},
//...