我正在尝试根据条件切换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'
Run Code Online (Sandbox Code Playgroud)
使用此代码,tagName不会更改,它始终保持div.模板切换正确.
任何帮助,将不胜感激.干杯!