ReferenceError:用户未定义rails + backbone + firebug

时间:2013-05-23 17:08:26

标签: ruby-on-rails backbone.js view firebug render

所以我问过这个问题:render view in backbone.js + rails

我觉得我已经接近答案了,因为有人指着我正确的方向。我的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});
    //$('body').html(newView.render().$el)
    console.log(newView.render().$el)
    //newView.render();
    Backbone.history.start();
  });
</script>

当我在尝试加载此页面后检查firebug控制台时,出现错误:

ReferenceError: users is not defined
[Break On This Error]   

window.newView = new Example.Views.Users.NewView({model: users});

以下是相关代码的其余部分。

资产/ Javascript角/骨干/视图/用户/ 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)

看看旧问题的评论,你几乎可以看到我来自哪里 - 以及我尝试过的。谢谢!

更新

user.js.coffee

class Example.Models.User extends Backbone.Model
  paramRoot: 'user'

  defaults:
    name: null
    email: null

class Example.Collections.UsersCollection extends Backbone.Collection
  model: Example.Models.User
  url: '/users'

2 个答案:

答案 0 :(得分:0)

与错误一样,用户未在new Example.Views.Users.NewView({model: users});中定义。

在home_controller.rb中,你应该有:

def index
  @users = User.all
end

应用程序/视图/家/ index.html.erb:

...
window.newView = new Example.Views.Users.NewView({model: <%= @users.to_json.html_safe -%>});
...

希望这有帮助!

答案 1 :(得分:0)

在页面加载的初始阶段,您似乎正在创建NewView对象。 window.newView = new Example.Views.Users.NewView({model: users});当它转到NewView cosutructor时,它找不到@collection,因为你没有传递NewView的参数。

您应该在页面加载时初始化路由器,然后按照Rails中Backbone的标准示例启动骨干历史记录。我有一个Example in Github可以正常工作。请查看它并遵循一些骨干文档。如果你想让它工作,那么你可以放window.newView = new Example.Views.Users.NewView({model: users,collection: <CollectionObject>});

希望这有帮助!!!