0

これは、マリオネットの使用をまだ混乱させていることの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)
4

1 に答える 1

2

構造

あなたのアプリは一般的に私がするように構成されています。ただし、変更する点がいくつかあります。

たとえば、「layout:rendered」のイベントバインディングを設定するために初期化子を使用する必要はありません。代わりにこれを行うことができます:

App.vent.bind "layout:rendered", -> CommentApp.showComments(comments)

この場合、初期化子の内部でイベントバインディングを設定することに実際の値はありません。

レイアウト

以下のlayout.listRegionが.show()を呼び出そうとすると、undefinedが返されるのはなぜですか?

...それについてはよくわかりません...このコードは少し奇妙に見えますが:App.layout.mainRegion.show(layout)

これは意図されたものでしたApp.mainRegion.show(layout)か?

レイアウトのリージョンは、レイアウトのrenderメソッドが呼び出されたときにインスタンス化されます。領域にレイアウトを表示すると、レイアウトのメソッドが呼び出され、レイアウトの領域にあるものを表示するためにレイアウトrenderのイベントをリッスンすると、show正常に機能するはずです。実際、私はこれをかなり定期的に行っています。

しかし、最近、これに似た問題の報告がいくつか見られました。ここのどこかに、特定できなかったバグがあると思います。

テンプレートは何comment_templateを参照していますか?これはどこかにプリコンパイルされたテンプレートですか?テンプレートのレンダリングをオーバーライドしましたか?もしそうなら、これを処理するコードを投稿できますか?

レイアウトの「show」イベントハンドラーの内部を実行するconsole.dir(layout)と、何が返されますか?リージョンオブジェクトが表示されますか?そのオブジェクトに他に何が見えますか?

于 2012-05-24T04:27:40.530 に答える