2

好吧,我很困惑。我正在尝试设置 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.coffee

#= 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()

entry_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 文件中除该行之外的所有内容 - 当我转到根页面时……它只是说“正在加载……”,这只是之前用作占位符的代码的反映应用程序初始化。

也许是一双新鲜的眼睛……

更新

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

轨道:骨干轨道宝石-

在看到这个和其他一些相关帖子之后......我尝试删除 //=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 .
4

1 回答 1

1

在coffeescript.org上的“Try Coffeescript”中检查您的entry.js.coffee代码,我得到了同样的错误

添加{}defaults:清除错误,现在可以正确呈现

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'
于 2013-05-17T21:23:43.390 回答