これは、マリオネットの使用をまだ混乱させていることの1つです。
コメントのリストを表示し、新しいコメントを作成するためのシンプルなインターフェイスを想像してみてください。私が現在これを構築している方法は、CommentApp
私が以下に含めたようなものです。
私の質問には2つの部分があると思います。
これは、このタイプのアプリケーションを構築する方法ですか?可能な場合はBBCloneMailサンプルアプリをフォローしようとしましたが、コレクションに新しいアイテムを作成する例が提供されていないようです。
なぜ私がそれを呼んでみたところにlayout.listRegion
以下が戻ってくるのですか?より一般的には、ネストされたレイアウトでイベントのトリガーとバインドを処理する決定的な方法があります。かなり混乱しているようです。undefined
.show()
'layout:rendered'
App.module "CommentApp", (CommentApp, App, B, M, $, _) ->
class Layout extends M.Layout
template: 'comment_layout'
regions:
listRegion: '#comment_list_region'
newRegion: '#new_comment_region'
class CommentView extends M.ItemView
# this is as you would expect
class NewCommentView extends M.ItemView
# this is as you would expect
# This view triggers a 'new:comment' on the CommentApp.vent
# event aggregator when the user submits a new comment.
class CommentsListView extends M.CollectionView
# this is as you would expect
# This view listens to the 'new:comment' event on the
# CommentApp.vent event aggregator.
# Public API
# ----------
CommentApp.showComments = (comments) ->
# In here I need to render a layout
layout = new Layout()
# I also need to render a comments list an the
# view for writing a new comment.
commentsListView = new CommentsList(collection: comments)
newCommentView = new NewCommentView()
# THen I need to place these views into the layout when ready.
layout.on "show", ->
# This part fails because it seems that the layout.listRegion
# is not yet defined for some reason. Don't understand why?
layout.listRegion.show(commentsListView)
layout.newRegion.show(newCommentView)
# Now I need to stick the commenting layout into it's
# place in the main app layout.
App.layout.mainRegion.show(layout)
App.addInitializer (options) ->
@vent.bind "layout:rendered", -> CommentApp.showComments(comments)
'comment_layout'
テンプレートは単なる基本的なコンテナテンプレートです。
<h2>Comments</h2>
<section id="comment_list_region"></section>
<section id="new_comment_region"></section>
そして、私はJST
それをレンダリングするために使用しています。このコードでレンダリング関数をオーバーライドしました:
# Override the marionette renderer to make it use JST.
Backbone.Marionette.Renderer.render = (template, data) ->
# Check if a template is specified first.
if template
throw "Template '" + template + "' not found!" unless JST[template]
JST[template](data)