我会用一个清晰的问题开始,之后解释在递归结构:
如何正确封装的模型,包含在CompositeView
,为ItemView
渲染使用CompositeView中,而封装每个模型与木偶
我的问题很简单,但我无法设法得到一些好的工作。 我有一棵笔记。 每个音符是Backbone.model并具有@descendants
集合,然后 我有一个Backbone.Collection代表该树(其中@descendants是一个实例) 我设置基本CollectionView
,以作为itemView
,该CompositeView
。一切都完美呈现,我对此感到非常高兴。这个工作解决方案显示在第一个代码片段中!
问题来了。在此基础上,我有点碰到这个问题:Events fired for whole hierarchy 这对我的事件如何绑定是有意义的。起初,我只是添加了独特的选择器(添加了data-guid到我的html元素,所以我可以通过它得到它们),它运作正常,虽然它已经变得“哈克”。但其他类似的问题已经上升,所以我决定我需要一个解决方案。 因此,我告诉myslef,让我们将每个模型封装在一个ItemView中,并使用Composite视图递归地呈现它们。 但是......这并不是在所有的世界最好...
以下是我在开始时:
class Note.ModelView extends Marionette.CompositeView
template: "note/noteModel"
id: "note-item"
itemViewContainer: ".note-descendants"
ui:
noteContent: ".noteContent"
events:
"keypress .noteContent": "createNote"
"click .destroy": "deleteNote"
# ...
initialize: ->
@collection = @model.descendants
# ...
class Note.CollectionView extends Marionette.CollectionView
id: "note-list"
itemView: Note.ModelView
initialize: ->
@listenTo @collection, "sort", @render
现在我调到属于渲染模型的每一件事新ItemView控件
class Note.ModelView extends Marionette.ItemView
template: "note/noteModel"
ui:
noteContent: ".noteContent"
events: ->
guid = @model.get 'guid'
events = {}
events["keypress #noteContent#{guid}"] = "createNote"
events["blur #noteContent#{guid}"] = "updateNote"
events["click #destroy#{guid}"] = @triggerEvent 'deleteNote'
initialize: ->
@bindKeyboardShortcuts()
@listenTo @model, "change:created_at", @setCursor
class Note.TreeView extends Marionette.CompositeView
template: "note/parentNote"
itemView: Note.ModelView
initialize: ->
@collection = @model.descendants
@listenTo @collection, "sort", @render
_.bindAll @, "renderItem"
renderItem: (model) ->
if model.get('parent_id') is 'root'
itemView = new Note.ModelView model: model
itemView.render()
@$el.append itemView.el
onRender: ->
@collection.each this.renderItem
@renderItem @model
appendHtml: (collectionView, itemView) ->
@model.descendants.each (note) =>
iv = new Note.ModelView model: note
iv.render()
@.$('.note-descendants').append iv.el
class Note.CollectionView extends Marionette.CollectionView
id: "note-list"
itemView: Note.TreeView
initialize: ->
@listenTo @collection, "sort", @render
和noteMode模板(parentNote只是一个空的模板现在)
<button class="btn btn-danger btn-xs untab">UN</button>
<button class="btn btn-success btn-xs tab">TAB</button>
<div class="noteContent">{{{indent}}}{{{title}}}</div>
<button class="destroy"></button>
<div class="note-descendants"></div> # That is where I'm putting the children notes
所以这几乎工作。但它很黑,而且它仍然不起作用。我一直在阅读所有文档,并且我已经阅读了Derick Bailey on nested structure and CompositeView和David Sulc on Nested Views这两个链接,但我仍然觉得我错过了一些重要的细节。
我在寻找什么,作为答案,是任何暗示或任何常用的方式来管理这与木偶。我真的在寻找一些干净的东西,因为这将成为应用程序构建的角色之一。
也许我在错误的地方搜索?任何事情都会受到欢迎!非常感谢你
祝你一切安好。 谢谢你的时间。
工作演示 - http://jsfiddle.net/derickbailey/AdWjU,细节 - http://lostechies.com/derickbailey/2012/04/05/composite-views-tree-structures-tables-and -more/ –
谢谢你链接已经在问题中的链接! – Mikechaos
对不起。 –