没有布局模板或JSON视图的流星铁路由器 [英] Meteor Iron-Router Without Layout Template or JSON View

查看:132
本文介绍了没有布局模板或JSON视图的流星铁路由器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Meteor Iron-Router 将数据呈现为JSON或仅将其原始"显示(没有布局模板)

Using Meteor Iron-Router how can I either render data as JSON or simply display it "raw" (without a layout template)

基本上,我希望能够执行以下操作:

Essentially I want to be able to do something like:

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

其中/raw/posts 将显示Posts(集合)的原始json.

where /raw/posts would display the raw json of Posts (the collection).

谢谢!

注释:

我知道 Meteor中的JSON端点 但是流星路由器已经停产,并且Iron-Router似乎没有JSON端点功能.

I'm aware of JSON endpoint in Meteor But meteor-router is discontinued and Iron-Router does not appear to have a JSON-end-point feature.

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

I also had a look at https://atmospherejs.com/package/collection-api but it does not suit my needs because I need the ability to select a subset of the fields of the collection/record.

推荐答案

制作原始布局模板

<template name="direct_layout">
    {{> yield}}
</template>

然后将其用作您的layoutTemplate即可直接使用您的模板.

Then use that as your layoutTemplate to directly use your template.

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

我不确定您是否将其用作实际代码的占位符.如果您打算使用JSON或实际的原始文本来呈现数据.您可能要考虑使用服务器端路由.您应该改为执行以下操作:

I'm not sure whether you're using this as a placeholder for your actual code. If you intend to render data using JSON or actual raw text. You might want to consider using server-side routes. You should do something like this instead:

请注意,这是服务器端代码,与上面在客户端运行的代码不同:

Note that this is server side code, unlike the above which runs on the client side:

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

Have a look at Server side rendering on Iron-Router: https://github.com/EventedMind/iron-router/blob/master/DOCS.md#server-side-routing

这篇关于没有布局模板或JSON视图的流星铁路由器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆