render view in backbone.js + rails

2019-08-24 21:43发布

问题:

I am trying to render the view "new_view.js.coffee" (a form to create users) on my root page. I am using the rails-backbone gem, so far I have this:

app/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>

Its basically a copy of this (from the 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();
  });

My new view is this:

assets/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

Here is my users router:

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)

The "new" template:

assets/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>

When I go to localhost:3000, all I see is "Loading..." (which I put in for the purpose of seeing whether or not the app starts). Again, how would I go about rendering the new_view in the #container div above?

回答1:

You have not appended your rendered content to body:

  $(function() {
    window.newView = new Example.Views.Users.NewView({model: users});
    $('body').html(newView.render().$el)
    // newView.render();
    Backbone.history.start();
  });