1

我有一个 Marionette.ItemView ,它使用 CSS 类以不同的方式设置新项目的样式:

class Happenator.Views.Option extends Backbone.Marionette.ItemView
  tagName: 'li'
  className: =>
    return 'new' if @model.isNew()
  initialize: ->
    @bindTo @model, "change", -> @render()

保存和更新模型后,一切都会刷新,但“新”类仍保留在“li”上。有没有一种好方法可以在更新时更新封闭标签的类?

4

1 回答 1

5

是的,“新”类保留在“li”上,因为实际上 BackboneclassName仅在初始化方法调用之前使用该属性。看看这个答案以获得更多解释。

但为什么不使用 jQuery.toggleClass.removeClass? 就像是

render: =>
    @$el.html(@template(@model.toJSON()))
    unless @model.isNew()
        @$el.removeClass('new')

http://jsfiddle.net/GX8WJ/21/

于 2012-06-21T01:01:41.133 回答