我有两个具有一对多关系的流星集合:建筑物和空间
在我的建筑页面上,我想显示与建筑相关的空间。
现在,我是这样做的:
buildingsRoute.coffee
BuildingController = RouteController.extend(template: "buildings")
Router.map ->
@route "building",
path: "/buildings/:_id"
waitOn: ->
subs.subscribe "allBuildings"
subs.subscribe "allSpaces"
data: ->
building: Buildings.findOne(@params._id)
spaces: Spaces.find({building_id: @params._id})
建筑.玉石:
template(name="building")
+with building
.building
.page-header.position-relative
h1.inline #{name}
.row
.col-xs-12
+spacesList
template(name="spacesList")
.widgets
.row
h1 Spaces
+each spaces
.col-xs-12.col-sm-6
.widget-box.widget-color-purple
.widget-header
a(href="{{pathFor 'space'}}") #{name}
.widget-body
p Content here
我猜这是行不通的,因为 spaceList 模板的数据上下文与 Iron Router 为构建定义的数据上下文不同。
我可以将“+each space”替换为“+each ../spaces”,但这对我来说似乎不是一个非常通用的解决方案(如果我想在另一个上下文中使用我的 spacelist 模板怎么办?)
所以我尝试在模板助手中定义数据上下文,如下所示:
Template.spacesList.helpers
spaces: Spaces.find({building_id: @params._id})
但我收到错误消息:
Spaces is not defined.
所以我有点困惑。正确实现嵌套路由的流星方法是什么?
谢谢你!
EDIT:
Spaces集合的定义:/models/space.coffee
@Spaces = new Meteor.Collection("spaces",
schema:
building_id:
type: String
label: "building_id"
max: 50
name:
type: String
label: "Name"
optional: true
max: 50
creation_date:
type: Date
label: "Creation date"
defaultValue: new Date()
)
出版物:/server/publications.coffee
# Buildings
Meteor.publish "allBuildings", ->
Buildings.find()
Meteor.publish "todayBuildings", ->
Buildings.find creation_date:
$gte: moment().startOf("day").toDate()
$lt: moment().add("days", 1).toDate()
# Publish a single item
Meteor.publish "singleBuilding", (id) ->
Buildings.find id
# Spaces
# Publish all items
Meteor.publish "allSpaces", ->
Spaces.find()
EDIT 2
经过一番研究,我终于想出了一个解决方案:
Template.spacesList.helpers
spaces: () ->
if Router._currentController.params._id
subs.subscribe "buildingSpaces", Router._currentController.params._id
Spaces.find()
else
subs.subscribe "allBuildings"
Spaces.find()
nbr_spaces: () ->
Spaces.find().count()
附有附加出版物:
# Publish all items for building
Meteor.publish "buildingSpaces", (building_id) ->
Spaces.find({building_id: building_id})
错误是:
- 事实上,空间定义没有包装到函数中
- 我用不太性感的 Router._currentController.params._id 替换了 @params._id,但我找不到任何快捷方式。
我仍然不知道这是否是管理嵌套路由的 Meteor(最佳)方法......
有更好的推荐吗?