如何通过Joosy获取相关资源?

时间:2013-06-24 16:26:16

标签: ruby-on-rails relationship joosy

我按照https://github.com/coshx/techlunches/tree/fdb70ff65997f9

中的教程创建了一个vanilla 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

1 个答案:

答案 0 :(得分:1)

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

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