Backbone:绑定到视图的包装 div
我有一个主干 IndexView,它为每个“任务”模型调用一个 TaskView。我想将一个事件绑定到包裹任务视图的 li 元素。例如,“className”属性是“task”,我想在“.task”上触发一个事件。
我可以从父 (IndexView) 视图绑定,但我想访问单击视图内的信息,我猜测这意味着事件应该绑定到子 TaskView(?)
(此外,DOM 类 在实际的任务模板中可以访问......只是不是包装.task li)
任何想法赞赏 - 下面的代码。
子视图
Backbonescaffolddemo.Views.Tasks ||= {}
class Backbonescaffolddemo.Views.Tasks.TaskView extends Backbone.View
template: JST["backbone/templates/tasks/task"]
tagName: "li"
className: "task"
events:
"click .task" : "demoMethod"
initialize: () ->
_.bindAll(this, 'demoMethod', 'render')
console.log @
#@showTask()
@el.id = 'sort_order_' + @model.get('id') if @model
destroy: () ->
@model.destroy()
this.remove()
return false
demoMethod: () ->
console.log 'Work dammit, work. Arghhh'
render: ->
$(@el).html(@template(@model.toJSON() ))
return this
父视图
Backbonescaffolddemo.Views.Tasks ||= {}
class Backbonescaffolddemo.Views.Tasks.IndexView extends Backbone.View
template: JST["backbone/templates/tasks/index"]
id: "taskList"
events:
"keyup #searchTasks" : "searchTasks"
initialize: () ->
_.bindAll(this, 'addOne', 'sortable', 'task_id_from_element', 'addAll', 'searchTasks', 'updateSort', 'sortable', 'render')
@options.tasks.bind('reset', @addAll)
@options.tasks.bind('reset', @sortable)
@options.tasks.bind('add', @render)
@updateSort()
@sortable()
#...
addAll: () ->
@options.tasks.each(@addOne)
addOne: (task) ->
view = new Backbonescaffolddemo.Views.Tasks.TaskView({model: task})
$(@el).append(view.render().el)
render: ->
$(@el).html(@template(tasks: @options.tasks.toJSON() ))
@addAll()
return this
I have a backbone IndexView which calls a TaskView for each 'task' model. I'd like to bind an event to the li element that wraps the taskview. So for example, the 'className' attribute is 'task', and I want to trigger an event on '.task'.
I can bind from the parent (IndexView) view, but I'd like access to information inside the clicked view, which I'm guessing means the event should be bound to the child TaskView(?)
(Also, DOM classes inside the actual tasks template can be accessed...just not the wrapping .task li)
Any ideas appreciated - code below.
CHILD VIEW
Backbonescaffolddemo.Views.Tasks ||= {}
class Backbonescaffolddemo.Views.Tasks.TaskView extends Backbone.View
template: JST["backbone/templates/tasks/task"]
tagName: "li"
className: "task"
events:
"click .task" : "demoMethod"
initialize: () ->
_.bindAll(this, 'demoMethod', 'render')
console.log @
#@showTask()
@el.id = 'sort_order_' + @model.get('id') if @model
destroy: () ->
@model.destroy()
this.remove()
return false
demoMethod: () ->
console.log 'Work dammit, work. Arghhh'
render: ->
$(@el).html(@template(@model.toJSON() ))
return this
PARENT VIEW
Backbonescaffolddemo.Views.Tasks ||= {}
class Backbonescaffolddemo.Views.Tasks.IndexView extends Backbone.View
template: JST["backbone/templates/tasks/index"]
id: "taskList"
events:
"keyup #searchTasks" : "searchTasks"
initialize: () ->
_.bindAll(this, 'addOne', 'sortable', 'task_id_from_element', 'addAll', 'searchTasks', 'updateSort', 'sortable', 'render')
@options.tasks.bind('reset', @addAll)
@options.tasks.bind('reset', @sortable)
@options.tasks.bind('add', @render)
@updateSort()
@sortable()
#...
addAll: () ->
@options.tasks.each(@addOne)
addOne: (task) ->
view = new Backbonescaffolddemo.Views.Tasks.TaskView({model: task})
$(@el).append(view.render().el)
render: ->
$(@el).html(@template(tasks: @options.tasks.toJSON() ))
@addAll()
return this
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
啊——找到了。直接的“click”:“demoMethod”(没有指定的 DOM 范围)就可以完成这项工作。
Ahh - found it. A straight-out "click" : "demoMethod" (with no specified DOM scope) does the job.
我不明白你在哪里添加了包装 TaskView 的 div。
默认情况下,Backbone 视图包装在
div
中,但由于您将 TaskView 上的tagName
设置为li
,因此它将包装在li
而不是div
。I don't see where you're adding the div that wraps the TaskView.
By default, Backbone views are wrapped in a
div
but since you set thetagName
on your TaskView toli
, it's going to be wrapped in ali
and not adiv
.