【问题标题】:Meteor Iron-Router Without Layout Template or JSON View没有布局模板或 JSON 视图的 Meteor Iron-Router
【发布时间】:2014-05-19 21:34:39
【问题描述】:

使用 Meteor Iron-Router 我如何将数据呈现为 JSON 或简单地“原始”显示(没有布局模板)

基本上我希望能够做类似的事情:

this.route('rawdata', {
  path: '/raw/:collection',
  layoutTemplate: 'nolayout',
  template: 'raw'
})

/raw/posts 将显示 Posts(集合)的原始 json。

谢谢!

注意事项

我知道JSON endpoint in Meteor 但是meteor-router 已经停产,而且Iron-Router 似乎没有JSON 端点功能。

我还查看了https://atmospherejs.com/package/collection-api,但它不适合我的需要,因为我需要能够选择集合/记录的字段子集。

【问题讨论】:

    标签: json meteor iron-router


    【解决方案1】:

    制作原始布局模板

    <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

    【讨论】:

    • 感谢 Akshat 提供更完整的回答。
    • @nelsonic 这个答案应该适合你。 reststop2 包基本上就是这样做的——它在 Iron-router 支持服务器端路由之前出现。如果您在响应类型、身份验证、路由、参数等方面需要更大的灵活性,请查看 reststop2。 github.differential.io/reststop2
    • 如何调用服务器端路由?是 localhost:3000/raw/:collection 吗?
    • @Nate 就像您为path 定义它并使用this.params.collection 一样
    • @Nate 它看起来不像是这段代码给出了问题,如果 Messages 未定义,您可能必须构建您的应用程序,以便在您定义它的位置 Messages = new Meteor.Coll... 在此之前运行一点代码就可以了。本质上,这个未定义的错误是因为它找不到您的 Messages 集合的定义
    【解决方案2】:

    见:https://github.com/EventedMind/iron-router/issues/114

    (注意修改内容类型的注释。)

    【讨论】:

    • 感谢@TK-421 +1 的链接
    猜你喜欢
    • 1970-01-01
    • 2019-03-01
    • 2014-01-04
    • 2013-11-23
    • 1970-01-01
    • 2014-05-26
    • 2015-08-31
    • 2014-01-16
    • 1970-01-01
    相关资源
    最近更新 更多