1

我按照https://github.com/coshx/techlunches/tree/fdb70ff65997f9中的教程创建了一个香草 joosy rails 应用程序

rails 和 joosy 方面都有两个资源:

# app/models/presenter.rb
class Presenter < ActiveRecord::Base
  attr_accessible :email, :github_username, :name, :twitter_username

  has_many :presentations
end

# app/models/presentation.rb
class Presentation < ActiveRecord::Base
  attr_accessible :description, :title

  belongs_to :presenter
end

# app/assets/javascripts/techlunches/resources/presentation.js.coffee
# (next line needed because this file is loaded before presenter.js.coffee)
#= require techlunches/resources/presenter
class @Presentation extends Joosy.Resource.REST
  @entity 'presentation'
  @map 'presenter', @Presenter

# app/assets/javascripts/techlunches/resources/presenter.js.coffee
class @Presenter extends Joosy.Resource.REST
  @entity 'presenter'
  @map 'presentations', @Presentation

当我访问主页时,控制台中会出现以下几行:

>> Presentation.find(1)('presenter_id')
   1
>> Presenter.find(1)('name')
   Ben

但是,这条线不起作用

>> Presentation.find(1)('presenter')
   undefined
4

1 回答 1

1

首先,资源是异步的——它们从后端请求数据。因此应该是Presentation.find 1, (presentation) -> presentation('presenter')

需要注意的另一件事是,此类请求将返回原始 JSON 数据。如果您在关联实例之后,请使用presentation.presenter.

于 2013-06-24T16:28:37.797 回答