TypeError:set在骨干关系中是未定义的

时间:2014-04-15 10:14:46

标签: ruby-on-rails backbone.js backbone-relational

使用rails中的骨干关系加载索引页面时出现奇怪错误 TypeError:set未定义

这是backbone-relational.js源码的一部分。错误指向1931行: 1891: var set = Backbone.Collection.prototype.__set = Backbone.Collection.prototype.set; ... 1931: var result = set.call( this, toAdd, _.defaults( { parse: false }, options ) );

这是我的代码:

路由器:

class Todo.Routers.ProjectsRouter extends Backbone.Router
  initialize: (options) ->
    @projects = new Todo.Collections.ProjectsCollection()
    @projects.reset options.projects

  routes:
    "new"      : "newProject"
    "index"    : "index"
    ":id/edit" : "edit"
    ":id"      : "show"
    ""        : "index"

  newProject: ->
    @view = new Todo.Views.Projects.NewView(collection: @projects)
    $("#projects").html(@view.render().el)

  index: ->
    @view = new Todo.Views.Projects.IndexView(projects: @projects)
    $("#projects").html(@view.render().el)

  show: (id) ->
    project = @projects.get(id)

    @view = new Todo.Views.Projects.ShowView(model: project)
    $("#projects").html(@view.render().el)

  edit: (id) ->
    project = @projects.get(id)

    @view = new Todo.Views.Projects.EditView(model: project)
    $("#projects").html(@view.render().el)

模型:

class Todo.Models.Task extends Backbone.RelationalModel
  paramRoot: 'task'

  defaults:
    title: null

class Todo.Collections.TasksCollection extends Backbone.Collection
  model: Todo.Models.Task
  url: '/tasks'

class Todo.Models.Project extends Backbone.RelationalModel
  paramRoot: 'project'

  defaults:
    title: null

  relations: [
    type: Backbone.HasMany
    key: 'tasks'
    relatedModel: 'Todo.Models.Task'
    collectionType: 'Todo.Collections.TasksCollection'
    includeInJSON: false
    reverseRelation:
      key: 'project_id'
      includeInJSON: 'id'
  ]

class Todo.Collections.ProjectsCollection extends Backbone.Collection
  model: Todo.Models.Project
  url: '/projects'

有什么建议吗?在index.html上我有

window.router = new Todo.Routers.ProjectsRouter({projects: #{@projects.to_json({:include => :tasks}).html_safe}});

似乎它出现在路由器

中的@projects.reset options.projects

1 个答案:

答案 0 :(得分:0)

它似乎是rubygems.org中rails-backbone gem的旧主干版本 要修复它,只需将主干更新到最新版本。如果您正在使用rails-backbone gem,请更改 gem 'rails-backbone' gem 'rails-backbone', git: "https://github.com/codebrew/backbone-rails.git" 在你的gemfile中