我正在尝试根据条件切换 Backbone 视图的 tagName。
我最初以为我可以设置一个默认的 tagName 说'div'(我意识到这是默认设置),然后在视图的初始化函数中,检查条件并更改 tagName 但不幸的是这不起作用。
这是我的视图代码(用coffeescript编写):
class Test.Views.ListView extends Backbone.View
attributes: {"data-role": "listview"}
className: 'row'
tagName: 'div'
initialize: ->
if @options and @options.device == "mobile"
@template = "/app/templates/mobile/test/test.html"
@tagName = 'ul'
使用此代码,tagName 不会更改,它始终保持为 div。而模板切换正确。
任何帮助,将不胜感激。干杯!