Meteor 從一個列表頁進入詳情頁如何快速顯示詳情,meteor列表詳情
不管是做android開發,還是做網頁web開發,都 會有列表,都需要點擊列表進入清單項目的詳情頁面,查看詳細資料,能常情況下,我們都是將這一清單項目的id傳到詳情頁,由詳情頁再到資料庫查詢詳細資料。
在用Meteor開發網站中,除了用傳id的方法,外還提供了一種簡單的方法,極大的方便了我們的開發,節省時間。
原文:http://blog.csdn.net/casun_li/article/details/46371811
1. 建立詳情頁的route 並傳資料:
(1)如可按傳id的方法,則這樣:
this.route('detail', { path: '/detail/:app_id', data: function() { return {app_id: this.params.app_id};//將id傳入到詳情頁面 }});(2)使用新方法(我將之叫做:傳對象的方法)我寫在後面的步驟都是按之來繼續的:
this.route('detail', { path: '/detail/:_id', onBeforeAction: function() { Meteor.subscribe("detailInId",this.params._id);//定閱 :定閱單條資訊 this.next(); }, data: function() { return appdocs.findOne({_id:this.params._id});//為詳情頁提供對象 //此處一定要用 findOne() 而不能用find()(它得到的是一個cursor對象) 如何一定要用則就是find({_id:_id}).fetch()[0] ,因為頁面用的是這個詳情的對象,而不是cursor對象 }});
2.伺服器的推送
Meteor.publish("detailInId", function (_id) { return appdocs.find({_id:_id});//此處要用find()頁不能用findOne() 因為publish提應該提供cursor 或 數組。});
3.詳情頁,點頁列表頁的連結<ahref="detail/{{_id}}">點擊查看</a> 進入詳情後,可以直接使用詳情對象來顯示內容了.例如
appdocs表中有name 和 url 這兩個欄位 ,則使用{{url}} {{name}}就能顯示內容,原因是在第一步的data中:return appdocs.findOne({_id:this.params._id}),meteor將它(為了理解,假設這個對象取名為:app)傳入了template detail中,
因此我們在調用{{name}}時就是調用了{{app.name}}
html:
<template name="detail">
<div class="form-group"> <label class="col-sm-2 control-label">:</label> <div class="col-sm-10"> <p class="form-control-static">{{url}}</p> </div></div><div class="form-group"> <label class="col-sm-2 control-label">應用程式名稱:</label> <div class="col-sm-10"> <p class="form-control-static">{{name}}</p> </div></div>
</template>
4. 簡單給出列表頁 html
<template name="applist"><table class="table table-striped table-hover"> <thead> <tr> <td >操作 </td> </tr> </thead> <tbody> {{#each apps}} {{> appitem}} {{/each}} </tbody></table>
</template><template name="appitem"> <tr > <td ><a href="detail/{{_id}}">點擊查看</a></td> </tr></template>
原文:http://blog.csdn.net/casun_li/article/details/46371811
參考:https://github.com/iron-meteor/iron-router/blob/master/Guide.md