Ruby on rails 如何通过Joosy获取相关资源?
我根据中的教程创建了一个香草joosy rails应用程序 rails和joosy端都有两个资源:Ruby on rails 如何通过Joosy获取相关资源?,ruby-on-rails,relationship,joosy,Ruby On Rails,Relationship,Joosy,我根据中的教程创建了一个香草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 clas
# 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')
undefined
首先,资源是异步的——它们从后端请求数据。因此,它应该是
Presentation.find 1,(Presentation)->Presentation('presenter')
另外需要注意的是,这样的请求将返回原始JSON数据。如果您要查找关联实例,请使用演示文稿。演示者首先,资源是异步的–它们从后端请求数据。因此,它应该是
Presentation.find 1,(Presentation)->Presentation('presenter')
另外需要注意的是,这样的请求将返回原始JSON数据。如果您要查找关联实例,请使用
presentation.presenter
presentation.find(1,函数(p){console.dir(p('presenter'));})
在本例中还显示了未定义的,因此当我切换到异步样式时,我看到了相同的结果。@BenTaitelbaum您的服务器响应什么?您是否将presenter
包含在控制器/视图的JSON响应中?我只是将presenter\u id
包含在JSON中,假设Joosy理解该约定。我是否需要显式地包含关联的资源?这可能是未来优化的一个领域,但目前您是否在rable、jbuilder或其他方面有偏好?Joosy总是对每个资源搜索发出唯一的HTTP请求。它试图确定服务器响应什么。所以,服务器有责任内联所有必需的关联@map
是一种糖,可以帮助对象的包装。如果您想发出子请求,实际上甚至不需要@map
,只需为每个对象调用find
两次即可。但你不应该。您只需使用的include
选项即可使用json
。但是我会推荐jbuilder
。谢谢,我使用了它,尽管我无法使用presentations.presenter('name')
Presentation.find(1,函数(p){console.dir(p('presenter');})
在本例中还显示了未定义的,因此当我切换到异步样式时,我看到了相同的结果。@BenTaitelbaum您的服务器响应什么?您是否将presenter
包含在控制器/视图的JSON响应中?我只是将presenter\u id
包含在JSON中,假设Joosy理解该约定。我是否需要显式地包含关联的资源?这可能是未来优化的一个领域,但目前您是否在rable、jbuilder或其他方面有偏好?Joosy总是对每个资源搜索发出唯一的HTTP请求。它试图确定服务器响应什么。所以,服务器有责任内联所有必需的关联@map
是一种糖,可以帮助对象的包装。如果您想发出子请求,实际上甚至不需要@map
,只需为每个对象调用find
两次即可。但你不应该。您只需使用的include
选项即可使用json
。但是我推荐使用jbuilder
。谢谢,我使用了它,尽管我无法使用演示文稿。演示者('name')
>> Presentation.find(1)('presenter')
undefined