1

バックボーン/レール アプリケーションのユーザー ビューを作成しています。新しいビューを追加しようとしています:

/views/users/edit.js.coffee

class MosaikBackbone.Views.UsersEdit extends Backbone.View

  template: JST['users/edit']

  render: ->
    $(@el).html(@template())
    this 

テンプレートは /templates/users/edit.jst.eco にあります

<h1>edit page</h1>

私のルーターは /routers/users.js.coffee で次のようになります。

class MosaikBackbone.Routers.Users extends Backbone.Router
    routes:
        '': 'index'
        'users/:id': 'show'
        'users/edit/:id': 'update'

    initialize: ->
        @collection = new MosaikBackbone.Collections.Users()
        @collection.fetch()

    index: ->
        view = new MosaikBackbone.Views.UsersIndex(collection: @collection)
        $('#container').html(view.render().el)

    show: (id) ->
        alert "Entry #{id}"

    update: (id) ->
        editView = new MosaikBackbone.Views.UsersEdit()
        $('#container').html(editView.render().el)

インデックス ビュー/テンプレートは正常に機能し、ビューはコンテナー div に挿入されています。UsersEdit ビューの tagName を「li」に変更すると、DOM に li 要素が表示されますが、テンプレートが読み込まれていません。構文の問題だと思いますが、コンソールにエラーはありません。

更新: 明確にするために#container、空の要素アイテムが表示されるため、ビューが追加されています。テンプレートのレンダリングに問題があります。

template: JST['users/index']UsersIndex ビューで参照すると、すべて正常に動作します。template: JST['users/edit']両方のテンプレートが同じフォルダーにあるにもかかわらず、何らかの理由で機能しません。また、編集テンプレートに無効な構文を配置できますが、エラーはスローされないため、検出されません。

更新 2: 次のビュー/テンプレートは問題なく動作します。

class MosaikBackbone.Views.UsersIndex extends Backbone.View

  template: JST['users/index']

  initialize: ->
    @collection.on('reset', @render, this)
    @collection.on('add', @appendUser, this)

  render: ->
    $(@el).html(@template())
    @collection.each(@appendUser)
    this

  appendUser: (user) ->
    view = new MosaikBackbone.Views.User(model: user)
    $('#users').append(view.render().el)

テンプレート/ユーザー/index.jst.eco

h1>title here</h1>
<ul id="users"></ul>
4

0 に答える 0