2014-04-07 143 views
3

使用流星鐵路由器哪能要麼數據呈現爲JSON或簡單地顯示它的「原始」(沒有佈局模板)流星鐵路由器沒有佈局模板或JSON查看

基本上我想成爲可以這樣做:

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

其中/生/職位將顯示帖子(集合)的原始JSON。

謝謝!

注意

我所知道的JSON endpoint in Meteor 但流星路由器停產,鐵路由器不會出現有一個JSON終點的功能。

我也看過https://atmospherejs.com/package/collection-api,但它不適合我的需要,因爲我需要能夠選擇集合/記錄的字段的子集。

回答

10

使原始佈局模板

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

看一看服務器端渲染上鐵路由器:https://github.com/EventedMind/iron-router/blob/master/DOCS.md#server-side-routing

+0

感謝您的更完整的答案Akshat。 – nelsonic

+1

@nelsonic這個答案應該適合你。 reststop2軟件包基本上就是這樣做的 - 它出現在鐵路由器支持的服務器端路由之前。如果您需要比響應類型更多的靈活性,認證,路由,參數等,請參閱reststop2。 http://github.differential.io/reststop2/ – gerard

+0

你如何調用服務器端路由?它是localhost:3000/raw /:集合嗎? – Nate