问题
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.
回答1:
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
回答2:
See: https://github.com/EventedMind/iron-router/issues/114
(Note the comment amending the content type.)
来源:https://stackoverflow.com/questions/22915857/meteor-iron-router-without-layout-template-or-json-view