ruby-on-rails - 使用 rails 设置 backbone.js 时为 "SyntaxError: unexpected }"

标签 ruby-on-rails ruby backbone.js syntax-error

<分区>

好吧,我很困惑。我正在尝试建立一个 rails/backbons SPA。我正在关注这个 railscast:http://railscasts.com/episodes/323-backbone-on-rails-part-1?autoplay=true

我在尝试访问根页面时从浏览器收到此错误:

ExecJS::RuntimeError in Main#index

Showing /Users/Eamon/raffle/raffler/app/views/layouts/application.html.erb where line #6      raised:

SyntaxError: unexpected }
  (in /Users/Eamon/raffle/raffler/app/assets/javascripts/backbone/models/entry.js.coffee)

Extracted source (around line #6):

3: <head>
4:   <title>Raffler</title>
5:   <%= stylesheet_link_tag    "application", :media => "all" %>
6:   <%= javascript_include_tag "application" %>
7:   <%= csrf_meta_tags %>
8: </head>
9: <body>

这是我的代码 - 我只看了几分钟视频。

raffler.js.咖啡

#= require_self
#= require_tree ./templates
#= require_tree ./models
#= require_tree ./views
#= require_tree ./routers

window.Raffler =
  Models: {}
  Collections: {}
  Routers: {}
  Views: {}
  init: -> 
    new Raffle.Routers.Entries()
    Backbone.history.start()

$(document).ready ->
  Raffler.init()

entries_router.js.coffee

class Raffler.Routers.EntriesRouter extends Backbone.Router
  initialize: (options) ->
    @entries = new Raffler.Collections.EntriesCollection()
    @entries.reset options.entries

  routes:
    "new"      : "newEntry"
    ''         : 'index'
    ":id/edit" : "edit"
    ":id"      : "show"

  newEntry: ->
    @view = new Raffler.Views.Entries.NewView(collection: @entries)
    $("#entries").html(@view.render().el)

  index: ->
    alert "home page"

  show: (id) ->
    entry = @entries.get(id)

    @view = new Raffler.Views.Entries.ShowView(model: entry)
    $("#entries").html(@view.render().el)

  edit: (id) ->
    entry = @entries.get(id)

    @view = new Raffler.Views.Entries.EditView(model: entry)
    $("#entries").html(@view.render().el)

我知道上面的大部分代码在类型转换的这一点上是无关紧要的……它们都是由脚手架生成器创建的——我想我不必删除任何东西。

entry.js.coffee

class Raffler.Models.Entry extends Backbone.Model
  paramRoot: 'entry'

  defaults:

class Raffler.Collections.EntriesCollection extends Backbone.Collection
  model: Raffler.Models.Entry
  url: '/entries'

以上文件是我认为发生错误的地方。我似乎无法在任何地方找到语法错​​误。我注意到在转换页面上与 railscast 一起使用的代码中,entry.js.coffee 只是有

class Raffler.Models.Entry extends Backbone.Model

我尝试删除除 entry.js.coffee 文件的那一行之外的所有内容 - 当我转到根页面时......它只是说“正在加载......”,这只是反射(reflect)了用作应用初始化前的占位符。

也许有一双新鲜的眼睛......

更新

我在这里找到了一个有类似问题的人:

rails: backbone-on-rails gem-

在看到这个和其他一些相关的帖子之后...我尝试删除//=require_tree 。来自 application.js 的行。其他一些帖子说它需要在底部......但我的已经是,所以这也不是问题。如果相关,这是我的 application.js 文件:

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require underscore
//= require backbone
//= require backbone_rails_sync
//= require backbone_datalink
//= require backbone/raffler
//= require_tree .

最佳答案

coffeescript.org 上的“尝试 Coffeescript”中检查您的 entry.js.coffee 代码, 我得到 the same error .

{} 添加到 defaults: 清除了错误,现在 renders correctly .

entry.js.coffee

class Raffler.Models.Entry extends Backbone.Model
  paramRoot: 'entry'

  defaults: {}

class Raffler.Collections.EntriesCollection extends Backbone.Collection
  model: Raffler.Models.Entry
  url: '/entries'

关于ruby-on-rails - 使用 rails 设置 backbone.js 时为 "SyntaxError: unexpected }",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16617928/

相关文章:

ruby-on-rails - Rails 更改默认布局和 View 路径

ruby-on-rails - 如何在 Rails 测试环境中获取完整堆栈跟踪而不是 "Unexpected error while processing request"?

jquery - 使用单个 div 作为丢失事件绑定(bind)的多个 Backbone View 的容器

ruby-on-rails - 在生产环境中使用 rails 设置 sunspot solr

ruby-on-rails - Rails 4 找不到关联 has_many,通过 : relationship error

arrays - 是否有返回两个子数组 : one of specified size and another of the remaining contents? 的 Ruby Array 方法

ruby-on-rails - 是否可以避免使用 Rails 3 加载 action_controller?

ruby-on-rails - 是否有可用的 Ruby on Rails 站点缩略图生成器?

javascript - : $(this. el).html 和 this.$el.html 有什么区别

backbone.js - 嵌套 Marionette 布局不呈现