的ReferenceError:用户没有定义轨+骨干+萤火虫 [英] ReferenceError: users is not defined rails + backbone + firebug

查看:170
本文介绍了的ReferenceError:用户没有定义轨+骨干+萤火虫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

于是我问这个问题:渲染视图Backbone.js的中轨+

我觉得我已经得到接近的答案感谢的人指着我在正确的方向。在code在我的应用程序/视图/家/ index.html.erb:

 < D​​IV ID =容器>加载...< / DIV><脚本类型=文/ JavaScript的>
  $(函数(){
    window.newView =新Example.Views.Users.NewView({模式:用户});
    //$('body').html(newView.render().$el)
    的console.log(newView.render()。$ EL)
    //newView.render();
    Backbone.history.start();
  });
< / SCRIPT>

当我审视尝试加载该页面后,Firebug控制台,我得到的错误:

 的ReferenceError:用户没有定义
[打破这个错误]window.newView =新Example.Views.Users.NewView({模式:用户});

下面是相关code的其余部分。

资产/ JavaScript的/骨干网/视图/用户/ new_view.js.coffee

  Example.Views.Users || = {}类Example.Views.Users.NewView扩展Backbone.View
  模板:JST [骨干/模板/用户/新]  事件:
    提交#新用户:救  构造器:(选项) - GT;
    超(选项)
    @model =新的@ collection.model()    @ model.bind(变化:错误,()=>
      this.render()
    )  节省:(E) - GT;
    即preventDefault()
    e.stopPropagation()    @ model.unset(错误)    @ collection.create(@ model.toJSON()
      成功:(用户)=>
        @model =用户
        window.location.hash =/#{@model.id}      错误:(用户,jqXHR)=>
        @ model.set({错误:$ .parseJSON(jqXHR.responseText)})
    )  渲染: - >
    $(@ EL)的.html(@Template(@ model.toJSON()))    这个$(​​形式)。backboneLink(@model)    返回此

users_router.js.coffee

 类Example.Routers.UsersRouter扩展Backbone.Router
  初始化:(选项) - GT;
    @users =新Example.Collections.UsersCollection()
    @ users.reset opt​​ions.users  路线:
    新:NEWUSER
    指数:指数
    :ID /编辑:​​编辑
    :标识:秀
    *:索引  NEWUSER: - >
    @view =新Example.Views.Users.NewView(集合:@users)
    $(#用户)。HTML(@ view.render()。EL)  索引: - >
    @view =新Example.Views.Users.IndexView(用户:@users)
    $(#用户)。HTML(@ view.render()。EL)  显示:(ID) - GT;
    用户= @ users.get(id)的    @view =新Example.Views.Users.ShowView(型号:用户)
    $(#用户)。HTML(@ view.render()。EL)  编辑:(ID) - GT;
    用户= @ users.get(id)的    @view =新Example.Views.Users.EditView(型号:用户)
    $(#用户)。HTML(@ view.render()。EL)

看评论的老问题,你可以pretty多看到我来自哪里 - 什么,我都试过了。谢谢!

更新

user.js.coffee

 类Example.Models.User扩展Backbone.Model
  paramRoot:用户  默认值:
    名称:空
    电子邮件:空类Example.Collections.UsersCollection扩展Backbone.Collection
  型号:Example.Models.User
  网址:'/用户


像错误说,用户没有在定义的新Example.Views.Users.NewView({模式:用户});

在你home_controller.rb你应该是这样的:

 高清指数
  @users = User.all
结束

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

  ...
window.newView =新Example.Views.Users.NewView({型号:LT;%= @ users.to_json.html_safe - %GT;});
...

希望这有助于!

So I asked this question: render view in backbone.js + rails

I feel I have gotten close to the answer thanks to people pointing me in the right direction. The code in my 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>

When I examine the firebug console after trying to load this page, I get the error:

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

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

Here is the rest of the relevant code.

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)

Look at the comments to the old question, and you can pretty much see where I'm coming from - and what I have tried. Thanks!

UPDATE

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'

解决方案

Like the error said, users is not defined in new Example.Views.Users.NewView({model: users});.

In your home_controller.rb you should have something like:

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 -%>});
...

Hope this helps!

这篇关于的ReferenceError:用户没有定义轨+骨干+萤火虫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆