Meteor Iron-Router Without Layout Template or JSON View
Asked Answered
T

2

3

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'
})

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

Thanks!

Notes:

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.

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.

Theater answered 7/4, 2014 at 14:50 Comment(0)
Z
10

Make a raw layout template

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

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

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

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));
  }
});

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

Zinkenite answered 7/4, 2014 at 15:11 Comment(6)
Thanks for the more complete answer Akshat.Theater
@Theater This answer should work for you. The reststop2 package basically does exactly this - it came out before iron-router supported server-side routes. If you need more flexibility over response type, authentication, routing, parameters, etc. take a look at reststop2. github.differential.io/reststop2Wench
How do you call a server side route? Is it localhost:3000/raw/:collection ?Elvaelvah
@Elvaelvah Just like how you've defined it for path and use this.params.collectionZinkenite
I am trying to get your server side code to work, which I am able to do. However I don't understand how I would pass data into the action function. If I try to define data=Messages.findOne() it returns that messages is not defined. Thanks for your help!Elvaelvah
@Elvaelvah it doesn't look like this code is the one giving the issues, if Messages is undefined you might have to structure your app so that where you defined it Messages = new Meteor.Coll... runs before this bit of code does. In essence this undefined error is because it can't find the definition for your Messages collectionZinkenite
M
2

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

(Note the comment amending the content type.)

Mcgrath answered 7/4, 2014 at 15:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.