json - 没有布局模板或 JSON View 的 Meteor Iron-Router

标签 json meteor iron-router

使用 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/

相关文章:

javascript - 如何使用 meteorjs 从集合中创建文本文件并将其保存在 SD 中

javascript - Meteor - 全局 keydown 后找到集合?

meteor - 下一条路线不是从页面顶部打开(向下滚动)。为什么?

Meteor.userId 只能在方法调用中调用。 ( meteor 铁路由器)

java - RecyclerView 不显示任何内容

meteor : One database per tenant in multi-tenancy app

javascript - JSONarray 没有数组名称?

javascript - meteorjs iron-router 和空链接标签

json - Perl LWP::UserAgent 解析响应 JSON

java - 如何编写一个基本的 JSON 解析类