使用 Meteor Iron-Router 如何将数据呈现为 JSON 或简单地将其显示为“原始”(没有布局模板)
本质上,我希望能够执行以下操作:
this.route('rawdata', {
path: '/raw/:collection',
layoutTemplate: 'nolayout',
template: 'raw'
})
在哪里 /原始/帖子将显示 Posts (集合)的原始 json。
谢谢!
备注 :
我知道 JSON endpoint in Meteor
但是meteor-router 已经停产,而且Iron-Router 似乎没有JSON 端点功能。
我还看了https://atmospherejs.com/package/collection-api但它不适合我的需要,因为我需要能够选择集合/记录的字段子集。
最佳答案
制作原始布局模板
<template name="direct_layout">
{{> yield}}
</template>
然后将其用作您的 layoutTemplate 以直接使用您的模板。
this.route('rawdata', {
path: '/raw/:collection',
layoutTemplate: 'direct_layout',
template: 'raw'
});
我不确定您是否将其用作实际代码的占位符。如果您打算使用 JSON 或实际的原始文本呈现数据。您可能需要考虑使用服务器端路由。你应该这样做:
请注意,这是服务器端代码,与上面在客户端运行的代码不同:
this.route('serverRoute', {
where: 'server',
path: '/your-route',
action: function() {
var data = {this_is:'a json object'}
this.response.writeHead(200, {'Content-Type': 'application/json'});
this.response.end(JSON.stringify(data));
}
});
查看 Iron-Router 上的服务器端渲染:https://github.com/EventedMind/iron-router/blob/master/DOCS.md#server-side-routing
关于json - 没有布局模板或 JSON View 的 Meteor Iron-Router,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22915857/