ReferenceError:用户未定义 rails + 主干 + firebug
Posted
技术标签:
【中文标题】ReferenceError:用户未定义 rails + 主干 + firebug【英文标题】:ReferenceError: users is not defined rails + backbone + firebug 【发布时间】:2013-05-23 17:08:25 【问题描述】:所以我问了这个问题: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);
这是其余的相关代码。
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
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)
查看旧问题的 cmets,您几乎可以看到我来自哪里 - 以及我尝试了什么。谢谢!
更新
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'
【问题讨论】:
【参考方案1】:就像错误说的那样,new Example.Views.Users.NewView(model: users);
中没有定义用户。
在你的 home_controller.rb 中你应该有类似的东西:
def index
@users = User.all
end
app/views/home/index.html.erb:
...
window.newView = new Example.Views.Users.NewView(model: <%= @users.to_json.html_safe -%>);
...
希望这会有所帮助!
【讨论】:
很酷,谢谢!现在更近了 - 这是我现在在 index.html.erb 中的内容:<div id="container">Loading...</div> <script type="text/javascript"> $(function() window.newView = new Example.Views.Users.NewView(model: <%= @users.to_json.html_safe -%>); $('body').html(newView.render().$el) Backbone.history.start(); ); </script>
收到此错误:TypeError: this.collection is undefined [Break On This Error] this.model = new this.collection.model();
@collection
定义在哪里?
我认为它只是在 new_view.js.coffee 中?...还是在路由器中?
我需要在我的家庭控制器中声明@collection吗?【参考方案2】:
看起来您是在页面加载的初始阶段创建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>);
希望对你有帮助!!!
【讨论】:
我不明白window.newView = new Example.Views.Users.NewView(model: <%= @users.to_json.html_safe -%>,collection: new Example.Collections.UsersCollection());
现在我收到一个错误,上面写着TypeError: Backbone.history is undefined [Break On This Error] Backbone.history.start();
也...我不能做“模型:用户”-它不起作用...它抛出错误@987654329 @
好的...我取得了突破...现在显示视图-我有一些语法错误...但我仍然收到错误TypeError: Backbone.history is undefined [Break On This Error] Backbone.history.start();
以上是关于ReferenceError:用户未定义 rails + 主干 + firebug的主要内容,如果未能解决你的问题,请参考以下文章
Ruby on Rails - 未捕获的 ReferenceError:$ 未定义
Ruby on Rails - 未捕获的ReferenceError:$未定义
RoR 3.2.6:未捕获的 ReferenceError:未定义 jQuery
尝试在 application.js 中导入 npm 包 (easyMDE) 时出错 - (未捕获的 ReferenceError:EasyMDE 未定义)