0

我正在尝试在我的根页面上呈现视图“new_view.js.coffee”(创建用户的表单)。我正在使用 rails-backbone gem,到目前为止我有这个:

应用程序/views/home/index.html.erb

<div id="container">Loading...</div>

<script type="text/javascript">
  $(function() {
    window.newView = new Example.Views.Users.NewView({model: users});
    newView.render();
    Backbone.history.start();
  });
</script>

它基本上是这个的副本(来自 rails-backbone README.md):

app/views/posts/index.html.erb

<div id="posts"></div>

<script type="text/javascript">
  $(function() {
    // Blog is the app name
    window.router = new Example.Routers.PostsRouter({posts: <%= @posts.to_json.html_safe -%>});
    Backbone.history.start();
  });

我的新观点是这样的:

资产/javascripts/backbone/views/users/new_view.js.coffee

Example.Views.Users ||= {}

class Example.Views.Users.NewView extends Backbone.View
  template: JST["backbone/templates/users/new"]

  events:
    "submit #new-user": "save"

  constructor: (options) ->
    super(options)
    @model = new @collection.model()

    @model.bind("change:errors", () =>
      this.render()
    )

  save: (e) ->
    e.preventDefault()
    e.stopPropagation()

    @model.unset("errors")

    @collection.create(@model.toJSON(),
      success: (user) =>
        @model = user
        window.location.hash = "/#{@model.id}"

      error: (user, jqXHR) =>
        @model.set({errors: $.parseJSON(jqXHR.responseText)})
    )

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

    this.$("form").backboneLink(@model)

    return this

这是我的用户路由器:

users_router.js.coffee

class Example.Routers.UsersRouter extends Backbone.Router
  initialize: (options) ->
    @users = new Example.Collections.UsersCollection()
    @users.reset options.users

  routes:
    "new"      : "newUser"
    "index"    : "index"
    ":id/edit" : "edit"
    ":id"      : "show"
    ".*"        : "index"

  newUser: ->
    @view = new Example.Views.Users.NewView(collection: @users)
    $("#users").html(@view.render().el)

  index: ->
    @view = new Example.Views.Users.IndexView(users: @users)
    $("#users").html(@view.render().el)

  show: (id) ->
    user = @users.get(id)

    @view = new Example.Views.Users.ShowView(model: user)
    $("#users").html(@view.render().el)

  edit: (id) ->
    user = @users.get(id)

    @view = new Example.Views.Users.EditView(model: user)
    $("#users").html(@view.render().el)

“新”模板:

资产/javascripts/backbone/templates/users/new.jst.ejs

<h1>New user</h1>

<form id="new-user" name="user">
  <div class="field">
    <label for="name"> name:</label>
    <input type="text" name="name" id="name" value="<%= name %>" >
  </div>

  <div class="field">
    <label for="email"> email:</label>
    <input type="text" name="email" id="email" value="<%= email %>" >
  </div>

  <div class="actions">
    <input type="submit" value="Create User" />
  </div>

</form>

<a href="#/index">Back</a>

当我转到 localhost:3000 时,我看到的只是“正在加载...”(我输入它是为了查看应用程序是否启动)。同样,我将如何在上面的#container div 中渲染 new_view?

4

1 回答 1

0

您尚未将呈现的内容附加到正文:

  $(function() {
    window.newView = new Example.Views.Users.NewView({model: users});
    $('body').html(newView.render().$el)
    // newView.render();
    Backbone.history.start();
  });
于 2013-05-23T15:33:46.260 回答